can i get any help in my hw

This is a discussion on *help* within the **C Programming** forums, part of the General Programming Boards category; can i get any help in my hw...

- 04-30-2012 #1

- Join Date
- Apr 2012
- Posts
- 4

## help

can i get any help in my hw

- 04-30-2012 #2

- Join Date
- Apr 2012
- Posts
- 4

the problem is

Write a complete program that reads 11 integers from a file called ‘file.txt’ and stores them in an array. Your program should then remove the duplicated numbers, find and print the sum of all the even numbers in the array as well as the sum of all the odd numbers in the array and then decides and prints the which sum is larger.

Your program should at least have the following functions:

1- Function**remove_dup**which removes duplicated elements from the array.

2- Functionwhich takes an array and returns the sum of the even numbers in it.*even_sum*

3- Functionwhich takes an array and returns the sum of the odd numbers in it.*odd_sum*

__Example of a single run of the program:__

Array filled from a file: 1 15 7 2 13 12 7 15 4 8 2

Array after removing duplicated numbers: 1 15 7 2 13 12 4 8

Sum of even numbers = 26

Sum of odd numbers = 36

The sum of odd numbers is greater

- 04-30-2012 #3

- Join Date
- Apr 2012
- Posts
- 4

my code dosent work

Code:#include <stdio.h> #include <stdlib.h> #define j 11 void remove_dup(int [ ],int); int even_sum(int [ ],int); int odd_sum(int [ ],int); int main() { FILE *in; int a[j],s1,s2; in=fopen("nums.txt","r"); fscanf(in,"%d %d %d %d %d %d %d %d %d %d %d ",&a[0],&a[1],&a[2],&a[3],&a[4], &a[5],&a[6],&a[7],&a[8],&a[9],&a[10]); remove_dup(a,j); s1=even_sum(a,j); printf("The even sum = %d",s1); s2=odd_sum(a,j); printf("The odd sum = %d",s2); if (s1>s2) printf("The even sum is larger"); else printf("The odd sum is larger"); return 0; } void remove_dup(int a[ ], int n ) { int i,j,k; printf("array element are:"); for(i=0;i<n;i++) { printf("%d",a[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]==a[j]) { a[j]=0; } } } printf("\nafter deleting duplicate element are:"); for(i=0;i<n;i++) { if(a[i]==0) { continue; } else { printf("%d",a[i]); }}}} int even_sum(int a[ ],int j) { int sum1=0; for (i=0,i<j,i++) if a[i]%2==0; sum1=sum1+a[i]; return sum1; } int odd_sum(int a[],int j) { int sum2=0; for (i=0,i<j,i++) if a[i]%2!=0; sum2=sum2+a[i]; return sum2; }

- 04-30-2012 #4
So which is the first answer that is wrong - does it remove all the duplicates or not?

Some links to read

http://sourceforge.net/apps/mediawik...le=Indentation

http://www.catb.org/~esr/faqs/smart-...tml#bespecificIf you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

If at first you don't succeed, try writing your phone number on the exam paper.

I support http://www.ukip.org/ as the first necessary step to a free Europe.

- 04-30-2012 #5

- Join Date
- Apr 2012
- Posts
- 4

there is an error "syntax error befor numeric constant" line 26

- 04-30-2012 #6
First, you used j as both a macro constant and a variable name.

Second, your indentation is so awful that you missed an extra brace.

Code:#include <stdio.h> #include <stdlib.h> //!! pick a different name, one that isn't the same as a variable //!! typically, macro constants are in uppercase to aid identification //!! Try #define MAX_INPUT 11 #define j 11 void remove_dup(int[], int); int even_sum(int[], int); int odd_sum(int[], int); int main() { FILE *in; int a[j], s1, s2; in = fopen("nums.txt", "r"); fscanf(in, "%d %d %d %d %d %d %d %d %d %d %d ", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9], &a[10]); remove_dup(a, j); s1 = even_sum(a, j); printf("The even sum = %d", s1); s2 = odd_sum(a, j); printf("The odd sum = %d", s2); if (s1 > s2) printf("The even sum is larger"); else printf("The odd sum is larger"); return 0; } void remove_dup(int a[], int n) { int i, j, k; printf("array element are:"); for (i = 0; i < n; i++) { printf("%d", a[i]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[i] == a[j]) { a[j] = 0; } } } printf("\nafter deleting duplicate element are:"); for (i = 0; i < n; i++) { if (a[i] == 0) { continue; } else { printf("%d", a[i]); } } } //!! WTF is this extra } int even_sum(int a[], int j) { int sum1 = 0; for (i = 0, i < j, i++) if a [i] % 2 == 0; sum1 = sum1 + a[i]; return sum1; } int odd_sum(int a[], int j) { int sum2 = 0; for (i = 0, i < j, i++) if a [i] % 2 != 0; sum2 = sum2 + a[i]; return sum2; }

If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

If at first you don't succeed, try writing your phone number on the exam paper.

I support http://www.ukip.org/ as the first necessary step to a free Europe.

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011