Anyone willing to help a ANSI C novice
Author Message
Anyone willing to help a ANSI C novice

Hello.

I am just starting to learn ANSI C and am having difficulty making a program
work. The program reads in 5 grades to an array then needs to add the grades
in a seperate function and output the result. The result is obviously wrong.
Any help is much aprecited.

Thankyou.

Russell

Program follows---

#include<stdio.h>
int sum(int array[],int,int);/*Function prototype*/
int main(void)

{
int myarray[5];
int num1=0, num2=0;
int total;
total=sum(myarray,num1,num2);
printf("The sum is %d\n",total);
return 0;

Quote:
}

int sum(int array2[],int n1,int n2)
{
int sumresult;
int sum,x,i;

for(i=0;i<5;i++)
{

printf("Enter the grade: ");
scanf("%d", &array2[i]);

Quote:
}

for (x=0;x<4;x++);
{
sum = sum + array2[x];

}

sumresult=n1+n2+sum;
return sumresult;

Quote:
}

Fri, 23 Aug 2002 03:00:00 GMT
Anyone willing to help a ANSI C novice
Thankyou very much, its much apreciated.

Fri, 23 Aug 2002 03:00:00 GMT
Anyone willing to help a ANSI C novice

Here, you read 5 elements into the array

Quote:
>for(i=0;i<5;i++)
>{

> printf("Enter the grade: ");
> scanf("%d", &array2[i]);

>}

And here, you only sum 4 of them.

Quote:
> for (x=0;x<4;x++);
> {
>  sum = sum + array2[x];

> }

This sort of thing is why passing the array size as an argument would be a
good idea.  If for some reason you don't want to do that, using a #define
would be the next best thing.
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies

Fri, 23 Aug 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages

Powered by phpBB® Forum Software