Thread: Need help !!!!

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Need help !!!!

    I have created partial of the code which required to calculate the weighted average for some floating numbers. Currently I am stuck as the code required
    to prompt the user to enter four weights from keyboard. I am not sure how to proceed. The result should able to print out the data, weights & weighted average.



    #include <stdio.h>
    float x[4]={1.2, 2.4, 3.6, 4.8};
    float xAve=0.0;
    float Weight_Total=0.0;
    main()
    {
    int i;

    for(i=0;i<4;i++) Weight_Total+=Weight[i];
    if(Weight_Total!=1.0)
    {
    printf("Error\n");
    exit(1);
    }

    for(i=0;i<4;i++) xAve+=Weight[i]*x[i];
    printf("xAve=%.1f\n",xAve);
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    First use code tags. Then it keeps your indentation.
    Second your code won't compile. Because Weight is undefined.
    Read my signature.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Oops seems like i have fortten to declare weight in my code.
    I wanted to make use of pointer & array to prompt user to
    enter the weights but i don't know how to proceed.

    #include<stdio.h>

    float x[4]={1.2, 2.4, 3.6, 4.8};
    float xAve=0.0;
    float Weight_Total=0.0;
    double Weight[10];
    main()
    {
    int i;
    printf("Pls key in the weights: \n", Weight);
    scanf(“.1f”,&Weight);
    for(i=0; i<5; i++)
    {
    Weight_Total+=Weight[i];
    if(Weight_Total!=1.0)
    {
    printf("Error\n");
    exit(1);
    }
    }
    for(i=0;i<4;i++) xAve+=Weight[i]*x[i];
    printf("xAve=%.1f\n",xAve);
    getchar();
    }
    Last edited by celica75; 09-28-2006 at 11:55 PM. Reason: Added code tags and ran it through a beautifier.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by celica75
    Oops seems like i have fortten to declare weight in my code
    So why repost the same code (poorly, again)?
    Code:
    main()
    Step into the '80's, or '90's, or even this millenia at any time.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    well ... i am not good in C so I am not able to come out with good codes.
    Izzit possible to advice on that? Thank you.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Who's Izzit?

    You don't care enough to read the FAQs on this board... why should we care enough to help you?

    Read the FAQs and edit your posts above to reflect that you have read them. If you do not, then you wont get help.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    oops ... thanks for your enlightenment.
    as I am new to this forum & I will do some readup
    and improve on the codes

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *sigh* *smashes head on keyboard*

    1 - Press EDIT.
    2 - Type stuff, preferably something like [code] ... [/code]
    3 - Press PREVIEW
    4 - Look at the post.
    Code:
    if( post == sucky )
            goto step2;
    5 - Post.
    6 - Optionally keep pressing edit because you can't help yourself. Then go back to step 2.


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

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Interestingly, that second post of his actually contained code tags last time I saw it. Then this happened:
    Last edited by celica75 : Today at 01:55 AM. Reason: Added code tags and ran it through a beautifier.
    By added, do you mean removed?
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    This should be it

    Hello

    The code that I just should be what you need. You just have to modify it a little bit. But by all means this is not the most elegant method of doing this I originally wrote this program when i was learning arrays.

    Code:
     #include <stdio.h>
    
    #define SIZE 12
    
    int main()
    {
    	float r[SIZE], total=0, x;
    	int i;
    
    	printf("Enter the rainfall data in milimeters for the month of January: ");
    	scanf("%f",&r[0]);
    
    	printf("Enter the rainfall data in milimeters for the month of February: ");
    	scanf("%f",&r[1]);
    
    
    	printf("Enter the rainfall data in milimeters for the month of March: ");
    	scanf("%f",&r[2]);
    
    
    	printf("Enter the rainfall data in milimeters for the month of April: ");
    	scanf("%f",&r[3]);
    
    	printf("Enter the rainfall data in milimeters for the month of May: ");
    	scanf("%f",&r[4]);
    
    	printf("Enter the rainfall data in milimeters for the month of June: ");
    	scanf("%f",&r[5]);
    
    	printf("Enter the rainfall data in milimeters for the month of July: ");
    	scanf("%f",&r[6]);
    
    	printf("Enter the rainfall data in milimeters for the month of August: ");
    	scanf("%f",&r[7]);
    
    	printf("Enter the rainfall data in milimeters for the month of September: ");
    	scanf("%f",&r[8]);
    
    	printf("Enter the rainfall data in milimeters for the month of October: ");
    	scanf("%f",&r[9]);
    
    
    	printf("Enter the rainfall data in milimeters for the month of November: ");
    	scanf("%f",&r[10]);
    
    
    	printf("Enter the rainfall data in milimeters for the month of December: ");
    	scanf("%f",&r[11]);
    
    
    for(i=0; i<=SIZE-1; i++)
    {
    		x=(total+= r[i])/12;
    
    	}
    
    			printf("Total milimeters of rainfall during the year is %fmm\n",x);
    
    		
    
    		return 0;
    
    	}

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Millimeters

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code leaves a lot to be desired. Your loop, when you actually got around to using one, should just be:
    Code:
    for( i = 0; i < SIZE; i++ )
    Also, there's no need to divide ... ok that's just ugly. At first I thought you were dividing 12 times. You are, but I see also that you're assigning to x and total and... It sucks. It should simply be:
    Code:
    for( total = 0.0, i = 0; i < SIZE; i++ )
        total += r[ i ];
    x = total / SIZE;
    See how much more readable that is?

    It's nice to try to help people, but you sometimes the contribution doesn't really help.


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

Popular pages Recent additions subscribe to a feed