السسسلام عليكم ..
صبــآح و مسسسـآء الخير...
ما جيت لهالمنتدى الا و انا عارف اني ابلقى ضالتي فيه..
بغيت مسساعدتكم ي شباب في هـ البرنامج مو راضي يزبط معي
Write a program that reads the following data from a file “data.txt” and prints the sum of all odd number and even number individually. You must use one function with parameters and the function should return the two values: two sums (see below). The prototype of the function should be
void function_name( input parameters including pointers).
You read each of the value from the datafile in main function and pass this value to the function, then the function will check if the value is odd or even and update the corresponding sum variable.
11
29
12
30
40
89
90
67
50
31
The output will be as follows:
The sum of all odd numbers : 227
The sum of all even numbers : 289
وهـآذي محاولتي فيه بس مو راضي يشتغل البرنامج في الـ C++
>>
#include <stdio.h>
void read_num( int num, int *sum1, int *sum2);
int main(void)
{ int num, i;
int sum1, sum2;
FILE *infile;
infile = fopen("data.txt","r");
/*if (infile==NULL)
{printf("Error openning file\n");
system("pause");
exit (1);}*/
for(i=0; i != NULL ;i++)
fscanf(infile, "%d", &num);
read_num(num, &sum1, &sum2);
printf("The sum of all even numbers : %d\n", sum1);
printf("The sum of all odd numbers : %d\n", sum2);
fclose(infile);
system("pause");
return (0);
}
void read_num( int num, int* sum1, int* sum2){
*sum1=0, *sum2=0;
int i;
for(i=0 ; i != NULL; i++){
if(num/2 == 0)
*sum1 = *sum1 + num;
else
*sum2 = *sum2 + num;
}
}
تكفووون مسسـآعدتكم ي اعضاء المنتدى لاهنتوآآ
