please help in C

This is a discussion on please help in C within the C Programming forums, part of the General Programming Boards category; Hi, i'm a newbie to program in C....I'm suppose to write to program that will find the smallest, largest, and ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    please help in C

    Hi,
    i'm a newbie to program in C....I'm suppose to write to program that will find the smallest, largest, and average values in a collection of N numbers. Get the value of N before scanning each value in the collection of N numbers. Then compute and display both the range of values in the data collection and the standard deviation of the data collection...Standard deviation=sqrt((sum_squares / N) - average^2)

    here's my code that i've written...but i can't figure out a way to get the smallest and the largest number....I have tried to get the smallest but i can't do it...I got the average with no problem.....
    Any helps are appreciate...Thanks for reading this message...
    Attached Files Attached Files
    Last edited by kylix; 10-16-2002 at 05:35 PM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    urgent? urgent to who? certainly not urgent to me. i have other more pressing things on my mind. anyway, stupid internet explorer displays a mess when i bring up your attachment, so here is a fixed version (same exact code) that ie will not clobber.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    double standard_deviation (double sum_squares, double N, double average);
    
    int main()
    {
    	int count, N;
    	double num, sum, average;
    	double small;
    	double sum_squares, st_dev;
    
    
    	sum = 0.0;
    	average = 0.0;
    	count = 0;
    
    	printf("How many number?" );
    	scanf("%d",&N);
    	printf("Enter %d number and <Enter> after each> ", N);
    
    
    	while (count < N){
    		scanf("%lf",&num);
    		sum= sum + num;
    		average= sum / (double) N;
    		sum_squares = pow(sum, 2);
    		count = count + 1;
    	}
    	
    	small = num;
    	
    	while (++count < N){
    		scanf("%lf",&num);
    		if (small < num)
    		small = num;
    	}
    
    	/* function call */
    	st_dev = standard_deviation (sum_squares, N, average);
    
    	/* print the result */ 
    	printf("\nAverage=%.2f", average);
    	printf("\nSmallest=%.2f", small);
    	printf("\nStandard Deviation is %.2f\n", st_dev);
    
    	return (0);
    
    }
    
    
    double standard_deviation (double sum_squares, double N, double average)
    
    {
    	double stand_dev;
    	stand_dev = sqrt((sum_squares / N) - pow(average, 2));
    	return (stand_dev);
    
    }
    hello, internet!

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    sorry i didn't know the rule at this board...but i will try to follow it the best i can...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assuming you have the numbers in an array, to find the largest and / or the smallest, they use almost identical code. Here is the pseudo code for it:
    Code:
    type someVariable, someCounter;
    type someArray[SOME_SIZE];
    
    someVariable = someArray[0]
    for someCounter = 0; someCounter < SOME_SIZE; someCounter++
        if someVariable < someArray[someCounter]
            someVariable = someArray[someCounter]
    Basicly, go through each cell in the array. If you are finding the greatest, then compare that value in the array with some temproary variable. If the temporary value is greater, don't make the assigment. If the value in the array is greater than the temporary, then assign the value of that spot in the array to the temporary value.

    When you reach the end of the array, you'll have your greatest value stored in the temproary value.

    You can do the same thing for the minimum value.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    Thanks for the quick reply quzah but my teacher does not teach me array yet so i don't think he would be very happy if i use in this program....again, thanks...

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    Thanks alot Salem...Let me try it out...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21