Thread: Calculating the sum of two arrays simultanuosly

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Calculating the sum of two arrays simultanuosly

    hey guys this program has been giving a headache for days now.i created a void sumarrays function which when called with two arguments which are the two arrays sums them up and prints their sum . but when i call it in the main() , the program crashes although it runs .
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    I'm not opening That. Post it on the forum with the code tags.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    source code

    hey guys i am trying to post the code but i dunno know how can anyone help me please?

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    copy and paste the code between the code tags

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Post here is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    void sumarrays ( int a[], int b[]);
    
    int total;
    
    int main()
    {
        int array1[N], array2[N];
    
        printf("\nEnter the ages of students in your class: ");
    
        for ( int i =0; i < N; i++)
        {
            scanf("%d", &array1[i]);
            while ( array1[i] == '\n') break;
    
        }
    
        printf("\nEnter their corresponding marks.");
    
        for ( int u = 0 ; u < N; u++)
        {
            scanf("%d", &array2);
    
            while(array2[u] == '\n') break;
    
        }
    
        //This part calls the void function
         sumarrays ( array1, array2);
    
    
        return 0;
    }
    
    void sumarrays( int a[], int b[])
    {
        for (int i = 0; i < N; i++)
        {
            total = a[i] + b[i];
             while ( a[i] != '=');
    
        }
        printf("\nThe total is: %d", total);
    }

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    from line 18 when i hit the enter key it does not break out of the loop

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You are scanning for an int not a char. You should check if scanf successfully read an int if not break out the loop.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Infinite loop:
    Code:
    while ( a[i] != '=');
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nyah Check View Post
    from line 18 when i hit the enter key it does not break out of the loop
    Instead of checking the entered value, check the return value from scanf() ...
    Look it up in your compiler's documentation... don't have that? Get it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating GCD
    By roelof in forum C Programming
    Replies: 15
    Last Post: 05-27-2011, 10:40 AM
  2. Is calculating for Arrays faster then .txt files ?
    By Coding in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2007, 07:35 AM
  3. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  4. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 02:39 PM
  5. calculating elements of arrays
    By Kristin in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 10:38 AM