Thread: Two code with almost same code but one work one doesn't.(find average)

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    2

    Two code with almost same code but one work one doesn't.(find average)

    Why I'm doing is base on this question:
    Write a C program that accepts two itemís weight (floating points' values ) and number of purchase (floating points' values) and calculate the average value of the items.

    I change a little so that it can accept any amount of item's weight and key in those variable after the code run.


    this is the code with two function but I is not working:
    Code:
    #include<stdio.h>
    
    
    float average(int x, int *Item, float *Weight){
        float sum;
        int count;
    
    
        for(int a=0;a<x;a++){
            sum+=(Item[a])*(Weight[a]);
            count+=Item[a];
        }
        printf("%f\n",sum);
        printf("%d\n",count);
        
        return sum/count;
    
    
    }
    
    
    int main(){
        int num;
    
    
        printf("number of type: ");
        scanf("%d",&num);
    
    
        int num_item[num];
        float weight[num];
    
    
    
    
        for(int i=1;i<=num;i++){
            printf("No. of item%d: ",i);
            scanf("%d",&num_item[i-1]);
    
    
            printf("Weight of item%d: ",i);
            scanf("%f",&weight[i-1]);
        }
    
    
        float av=average(num,num_item,weight);
        
    
    
        printf("Average Value = %f",av);
    
    
        return 0;
    }
    After I combine two function into one function, I just work perfect.
    Code:
    #include<stdio.h>
    
    
    int main(){
        int num;
        float sum;
        int count;
        
    
    
        printf("number of type: ");
        scanf("%d",&num);
    
    
        int num_item[num];
        float weight[num];
    
    
    
    
        for(int i=1;i<=num;i++){
            printf("No. of item%d: ",i);
            scanf("%d",&num_item[i-1]);
    
    
            printf("Weight of item%d: ",i);
            scanf("%f",&weight[i-1]);
        }
        
        for(int a=0;a<num;a++){
            sum+=(num_item[a])*(weight[a]);
            count+=num_item[a];
        }
        printf("%f\n",sum);
        printf("%d\n",count);
    
    
        float average=sum/count;
    
    
        printf("%f\n",average);
        
    
    
        return 0;
    }
    I don't know what is the problem for the first code, can someone help me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    In the function named average, you forgot to initialise sum and count to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    2
    ou, I didn't release it.
    thanks you very much

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    72
    Hi, I was thinking that the uninitialized variables are initialized by the compiler to 0. No?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by Nikosant03
    Hi, I was thinking that the uninitialized variables are initialized by the compiler to 0. No?
    Not necessarily. If you do not provide an initialiser, then the variable would be zero initialised if it has static storage duration, i.e., if it was declared at file scope or declared static in a local scope. In this case the variables are non-static local variables, so they are not guaranteed to be zero initialised and hence may start off containing garbage. This is likely what happened in the first program, whereas in the second the memory that the variables occupied could just have happened to be zeroed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    72
    Great explanation thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why this code doesn't work?
    By cdummie in forum C Programming
    Replies: 3
    Last Post: 04-04-2015, 10:39 AM
  2. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  3. My code doesn't work, help please
    By BenBusby in forum C Programming
    Replies: 3
    Last Post: 01-24-2013, 06:14 AM
  4. C+P code use to work but now it doesn't!
    By CodeMonkey03 in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2012, 07:33 PM
  5. Example Code doesn't work!
    By Kayl669 in forum C Programming
    Replies: 8
    Last Post: 02-18-2010, 10:19 AM

Tags for this Thread