1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

please HELP

Discussion in 'C' started by jack999, Apr 21, 2006.

  1. jack999

    jack999 New Member

    Joined:
    Apr 21, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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:
    #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);
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. jack999

    jack999 New Member

    Joined:
    Apr 21, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thanks shabbir
    but what do u mean by 4 & 5
    and how can i write input and output file name
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    He meant is num variable used without being initialized. and probably he found the loop to be infinite.
     

Share This Page