please HELP

jack999's Avatar
Newbie Member
hi guys


i cant run the c++ and i have H.W yo submet
i want someone to check if the answer is correct

this is the question
Write a C-Program that does the following
• Read integer numbers from a file inp.dat
• Calculate the sum, average, and multiplicand of even numbers and store the result and the even numbers in output file out1.dat
• Calculate the sum, average, and multiplicand of odd numbers and stored the result and the odd numbers in output file out2.dat



answer
Code: C
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
main ()
{
    FILE        *inp,*outp1,*outp2    ;
    int         num,even_sum,odd_sum,even_product,odd_product,count_even=0,count_odd=0;
    double      even_average,odd_average   ;
   
   
   
    inp=fopen("C:\grades.txt","r");
    outp1=fopen("C:out1.txt","w");
    outp2=fopen("C:out2.txt","w");
   
   
    if(inp==NULL)
    {
        printf("cannot open file");                               
        exit(1) ;
    }
   
   
    even_sum=0   ;
    even_product=1;
    odd_sum=0;
    odd_product=1;
   
    for(fscanf(inp,"%d",num);num!=EOF;fscanf(inp,"%d",num));
    {
        if(num%2==0)
        {
            fprintf(outp1,"even=%d\n",num);
            even_sum+=num   ;
            even_product*=num
                count_even ++;
        }
       
       
        else
        {
            fprintf(outp2,"odd=%d\n",num);
            odd_sum+=num  ;
            odd_product*=num  ;
            count_odd ++;
        }
       
    }
   
    even_average=(even_sum)/count_even  ;
    odd_average=odd_sum/count_odd  ;
   
    fprintf(outp1,"the sum=%d\n the product=%d\n and the average=%lf",even_sum,even_product,even_average);
    fprintf(outp2,"the sum=%d\n the product=%d\n and the average=%lf",odd_sum,odd_product,odd_average);
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
As i see there are quite a few errors.

1. Input file name is not correct
2. Output file Path is not correct.
3. There is semicolon missing on even_product*=num;
4. num used without initilized.
5. for loop is an infinite loop.
0
jack999's Avatar
Newbie Member
thanks shabbir
but what do u mean by 4 & 5
and how can i write input and output file name
0
coderzone's Avatar, Join Date: Jul 2004
Team Leader
He meant is num variable used without being initialized. and probably he found the loop to be infinite.