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  
 
 [ 4 post ] 

 Relevant Pages 

1. Anyone willing to help me learn arrays?

2. Anyone willing to help???

3. Help!!! Novice CS Advice

4. anyone willing to help an extreme (1st day) novice?

5. Newbie: separate big .cs file into small .cs files

6. Novice question: ANSI/VT100 printer?

7. urgent! anyone willing to help?

8. Some interesting novice questions from a novice guy

9. newbe/cs student, need help w/ code

10. Compiler error CS 1595 - Help

11. yes I am willing to

12. I'm willing to program for you.

 

 
Powered by phpBB® Forum Software