help

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...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Angry help

    can i get any help in my hw

  2. #2
    Registered User
    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- Function even_sum which takes an array and returns the sum of the even numbers in it.
    3- Function odd_sum which takes an array and returns the sum of the odd numbers in it.

    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

  3. #3
    Registered User
    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;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    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#bespecific
    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.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    there is an error "syntax error befor numeric constant" line 26

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    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;
    }
    Read the //!! comments.
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21