Compare sum of 2 1D array

Page 1 of 1

5 Replies - 2204 Views - Last Post: 24 February 2013 - 06:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313318&amp;s=58a48e65fd1f3494e9985b70a2ea02c5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mikelino

Reputation: 1
• Posts: 25
• Joined: 03-November 12

Compare sum of 2 1D array

Posted 24 February 2013 - 05:43 AM

```#include <stdio.h>
#include <conio.h>
#define N 10

int calculateSum(int x[]);
void printLarger(int,int);

main(){
int a[N], b[N];
int sum=0;
int sumOfA, sumOfB;

sumOfA=calculateSum(a);
sumOfB=calculateSum(B)/>;
printLarger(sumOfA, sumOfB);
getch();
return 0;

}

{
int i;

printf("enter data items:\n");
for (i = 0; i<N; i++) {
scanf("%d", &x[i]);
}
printf("the value entered are\n\t");
for (i = 0; i<N; i++) {
printf("%d\t", x[i]);
}
print("\n");
}

int calculateSum(int x[]){
int i;
for (i=0; i<N; i++) {
sum=sum+x[i];
}
return (sum);
}

void printLarger(sumOfA, sumOfB) {
if (sumOfA > sumOfB) {
printf {"array A is larger than array B");
for (i=0;i<N;i++) {
printf("%d\t", (a[i]);
}
else
printf {"array B is larger than array A");
for (i = 0; i < N; i++) {
printf("%d\t", b[i]);
}
}

```

what is the error?forgive me noob

This post has been edited by mikelino: 24 February 2013 - 05:47 AM

Is This A Good Question/Topic? 0

Replies To: Compare sum of 2 1D array

#2 jimblumberg

Reputation: 5471
• Posts: 17,024
• Joined: 25-December 09

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:02 AM

What do you mean by "what is the error?"? Does your program compile without errors or warnings? If not then post the complete error messages exactly as they appear in your development environment. If it does compile, when you run it does it produce the desired output? If not explain exactly what is wrong with your program, ask specific questions.

Jim

#3 mikelino

Reputation: 1
• Posts: 25
• Joined: 03-November 12

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:23 AM

```#include <stdio.h>
#include <conio.h>
#define N 10

int calculateSum(int x[]);
void printLarger(int a[],int b[],int,int);

main(){
int a[N], b[N];
int sumOfA, sumOfB;

sumOfA=calculateSum(a);
sumOfB=calculateSum(B)/>;
printLarger(a,b,sumOfA,sumOfB);
getch();
return 0;

}

{
int i;

printf("enter data items:\n");
for (i = 0; i<N; i++) {
scanf("%d", &x[i]);
}
printf("the value entered are\n\t");
for (i = 0; i<N; i++) {
printf("%d\t", x[i]);
}
print("\n");
}

int calculateSum(int x[]){
int sum=0;
int i;
for (i=0; i<N; i++) {
sum+= x[i];
}
return (sum);
}

void printLarger(int a[],int b[],int sumOfA,int sumOfB) {
int i;

if (sumOfA > sumOfB)
printf("array A is larger than array B");
for (i = 0; i < N; i++) {
printf("%d\t",a[i]);
}
else if (sumOfA < sumOfB)
printf {"array B is larger than array A");
for (i = 0; i < N; i++) {
printf("%d \t",b[i]);
}
}

```

the program say:" C:\Dev-Cpp\sem4.2.c In function `printLarger':
55 C:\Dev-Cpp\sem4.2.c syntax error before "else" "
help me Jim

#4 mikelino

Reputation: 1
• Posts: 25
• Joined: 03-November 12

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:35 AM

Sorry Boss.
Sorry boss Jim.
I know what went wrong already.

#5 jimblumberg

Reputation: 5471
• Posts: 17,024
• Joined: 25-December 09

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:43 AM

Okay lets look at the function where the error was reported.

Look at the difference in formatting, first your code formatting:
```void printLarger(int a[],int b[],int sumOfA,int sumOfB) {
int i;

if (sumOfA > sumOfB)
printf("array A is larger than array B");
for (i = 0; i < N; i++) {
printf("%d\t",a[i]);
}
else if (sumOfA < sumOfB)
printf {"array B is larger than array A");
for (i = 0; i < N; i++) {
printf("%d \t",b[i]);
}
}
```

Now the same code reformatted.

```void printLarger(int a[],int b[],int sumOfA,int sumOfB)
{
int i;

if (sumOfA > sumOfB)
printf("array A is larger than array B");
for (i = 0; i < N; i++) {
printf("%d\t",a[i]);
}
else if (sumOfA < sumOfB)
printf {"array B is larger than array A");
for (i = 0; i < N; i++) {
printf("%d \t",b[i]);
}
}

```

Do you notice any difference? Where is the matching if statement for that else?

Developing a consistent indentation style and always using braces with your control structures will help avoid this kind of problem.

Jim

This post has been edited by jimblumberg: 24 February 2013 - 06:50 AM

#6 mikelino

Reputation: 1
• Posts: 25
• Joined: 03-November 12

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:51 AM

Boss, can i ask a question?
Besides helping people answer the coding how to get reputation?
i shame on it 0 reputation but still a student
self also not expert onlt....