/*
Implement the recursive, divide-and-conquer version of ADD-ODDS from
Homework 1, problem 5. You may write your code in C or C++, but cannot
use
global variables. In addition to correctness, programming style is also
important, so please write modular, well-documented code.
1. Implement the recursive, divide-and-conquer function
ADD-ODDS(A,p,r)
that returns the sum of the odd numbers found in the positive
integer
array A[p ... r].
2. The main part of your program will read in data from a file whose
name
is given as the first argument to your executable. The first line
of
the input file contains the number of lines (1-99) in the rest of
the
file. Each subsequent line begins with the number of elements
(1-99) in
the array followed by the elements (1-99). Your main program should
process each line one at a time by reading in the array, calling
ADD-ODDS, and then printing the result. For example,
> a.out /public/cse/2320-501/pgm1in
9
398
0
9
384
*/
/*Program1*/
/*name:Anan Tongprasith*/
/*course: cse2320 sec 501*/
/*compile: cc addodd.c*/
#include
/* The main program reads data from input file and put the data into an*/
/*array name "element." Then we call function addodds to recursively add*/
/*odd numbers from the array. */
main(int argc, char *argv[])
{ FILE *fp;
char newline[100],newc;
int numofline,i,element[100],a=0;
fp=fopen(argv[1],"r"); /*Open input file*/
if(fgets(newline,100,fp) != NULL) /*Read first line*/
{ numofline=atoi(newline); /*Num of line=first line*/
for (i=numofline;i>0;i-=1) /*Do it line by line*/
{
fgets(newline,100,fp); /*Read a raw data line*/
a=testme(element,newline); /*Put data into an array*/
printf("%d\n",addodds(1,a,element)); /*Addodds the array*/
}
}
else
return 0;
fclose(fp);
}
/* Function testme reads a data line and put the data into an array.*/
int testme(int *ele,char *new)
{ int i=-1,j=0,k=0,linelength=strlen(new);char *piece;
do
{
j=0;printf("\0");
do /*Loop for data separation*/
{ i++;
piece[j++]=new[i]; /*buffer=data*/
} while(new[i]!=' ' && new[i]!='\0'); /*check for
space between data and end of line*/
ele[k]=atoi(piece); /*Put data into an array*/
*piece='\0'; /*reset buffer*/
k=k+1;
} while(i               ( geocities.com/vienna/7079)                   ( geocities.com/vienna)