5 Replies - 1073 Views - Last Post: 24 February 2013 - 06:51 AM Rate Topic: -----

#1 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

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

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

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

void readData(int x[])
{
    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  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

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

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

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

void readData(int x[])
{
    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
Was This Post Helpful? 0
  • +
  • -

#4 mikelino  Icon User is offline

  • New D.I.C Head

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

Re: Compare sum of 2 1D array

Posted 24 February 2013 - 06:35 AM

Sorry Boss.
i get it right already.
closed the thread.
Sorry boss Jim.
I know what went wrong already. :withstupid:
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,434
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 :hammer:
self also not expert onlt....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1