Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-07-2005, 06:42 AM   #1 (permalink)
SweetOne
Registered User
 
Join Date: Oct 2005
Posts: 6
SweetOne is on a distinguished road
Program help

Thanks for your help. I'm gonna try and alter the input to see if it works when I play around with it. I'll get the hang of this some day soon

Last edited by SweetOne; 11-08-2005 at 10:49 AM.
SweetOne is offline   Reply With Quote
Old 11-07-2005, 09:59 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Normaly you would create an array to hold the values which you wan't the user to put in ie:
Code:
#include <stdio.h>
#define SIZE 10 /* you should use 10 temperatures */
#define FREEZING 32.0 /* temperature for freezing point */

 /* will ask user for 10 temperatures, returns number of reads above FREEZING */
int read_temperatures(double* arr, int size);

/* temperatures are usualy as doubles, thus a average will be double aswell
 * it will only average temperatures above FREEZING */
double calculate_average (double* arr, int size);

/* simple test function to display the temperatures read */
int print_temperatures(double* arr, int size);

int main()
{
    double temperatures[SIZE], average_temp=0.0;
    int count_positive=0;

    if(!(count_positive = read_temperatures(temperatures, SIZE)))
      printf("Error no temperatures above freezing point (%.2f) added\n", 
	     FREEZING);
    else
      if(!(average_temp = calculate_average(temperatures, SIZE)))
	printf("Error calculating average temperature\n");

    /* testing bed */
    if(!print_temperatures(temperatures, SIZE))
      printf("Error printing temperatures\n");
    if(count_positive)
      {
	printf("Positive temps: %d\n", count_positive);
	printf("Average temp: %.2f\n", average_temp);
      }
    return 0;
}

int read_temperatures(double* arr, int size)
{
  double value=0;
  int j, res=0;
  for(j=0; j<size; ++j)
    {
      printf("Temperature %d: ", j+1);
      fflush(stdout);
      if(0 <= scanf("%lf", &value))
	{
	  if(value > FREEZING)
	    ++res;
	  arr[j]=value;
	}
      else
	printf("Error reading input temperature %.2f\n", value);
    }
  return res;
}

double calculate_average(double* arr, int size)
{
  int j;
  double res=0.0;
  for(j=0; j<size; ++j)
    if(arr[j] > FREEZING)
      res+=arr[j];
  return (res/size);
}

int print_temperatures(double* arr, int size)
{
  int j;
  printf("[ ");
  for(j=0; j<size; ++j)
    printf("%.2f%c", arr[j], j<(size-1)?',':' ');
  printf("]\n");
  return j;
}
Now the readin function can be changed to fit your needs, I just made this to fit what ever I thought usefull.. I'm not trying to do your homework, but for one thing I would like to keep the submitted temperatures below freezingpoint.

Btw, it could be altered, so it would save the below sub-temperature submitted at the end of the array, then a search through acceptable temperatures would be [0-count_positive] and a search through sub-temperatures would be [count_positive-SIZE] but in order to change the functions for calculation of average, it would need to have a starting point aswell.
Anywho, I'm not here to do your homework, so that would be somethign you could play with if you feel like it.. I'm guessing the next assignment will probably try to challenge you with something like that.

Oh, and in your example, the
Code:
for(i=1;i<=10;i++)
  iTemp[i ]=0; {
if (i<32)
  continue;
}
should have been
Code:
for(i=1;i<=10;i++)
{
  iTemp[i ]=0; 
  if (i<32)
    continue;
}
in order to activate your continue statement, but anyway it won't be effective since you're comparing the i value to your 32, i will never exceed 10, and iTemp will consist of 0 values, and never get filled with any input from teh user, oh and take a look at my code for fitting to your needs, because your example won't work anyway.

//edit, I noticed the increment counter I frequently use fsck'ed the display up, it found that [ i] should start italic display, even within the code tags.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 11-07-2005 at 01:23 PM.
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with copying backwards rogue Standard C, C++ 9 04-24-2005 05:39 PM
Help with a simple program C++noob Standard C, C++ 1 03-24-2005 04:31 PM
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 07:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 01:58 PM


All times are GMT -8. The time now is 03:29 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting