One-dimension array SUM prolem

This is a discussion on One-dimension array SUM prolem within the C Programming forums, part of the General Programming Boards category; Im suppose to write a code using one-dimension array, of size 100 only, and output the array on screen. The ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    One-dimension array SUM prolem

    Im suppose to write a code using one-dimension array, of size 100 only, and output the array on screen. The user should imput the value n from the keyboard, and then calculate, and display the sum between -n to n ONLY.

    Code:
    #include <stdio.h>      
    #define SIZE 100
    
    int main(void)
    
    {
        int integer[SIZE], n, j, sum, sum1;
                
        printf("Enter an integer: ");
        while (scanf("%s", &integar[n]) !=EOF)
        
        for (n=0;n<100;++n)
        {
            scanf("d", &integer[n]);
            
            if(integars[n] == -1)
            {
                j=n;
                break;
            }
            
            j=100;
            
            for(j=0;j<n;++j){
            sum=sum+integer[n];
            printf("The sum is %d\n", sum);
            }
        }
        
        return (0);
    }
    I started it out, and its giving me a weird output of odd numbers. Im completely lost :S. Where am I going wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your sum variable is being used in an uninitialized state... and your compiler should be complaining about it. (You ignore your compiler's reports at your own peril!)

    Insert sum = 0; at line 23

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    ohh yeah how did I forget that!

    It says the program stopped working, when I enter an integer :S.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    That's because you told scanf to read in a string and try to store it in a cell of an integer array, indexed to some unknown value n.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    okay, I basically did it all over again, and yes im stuck. It hates me! I can perfectly enter integars, but Im not getting any outcomes.

    Code:
    #include<stdio.h> 
    #include<string.h>         
    #define SIZE 100
    
    int main(void)
    
    {
        int array[SIZE], i, n, j, sum=0;
    
        printf("Enter some integers:");
        for (n=0;n<100;++n)
        {
          scanf("%d", &array[n]);
    
          if(array[n] == -1)
           {
             j=n;
             break;
          }  
           
          j=100;
           
           }
            printf("%d", array[n]);
            while(i>j)
            ++i;
            
                for(j=0;j<n;++j){
                sum=sum+array[n];
                printf("The sum is %d\n", sum);
             
        }
        return (0);
    }
    Last edited by neveragn; 11-14-2011 at 06:39 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You set j to 100 or less, then ask if unknown value i is greater than j, increment unknown value of i, then set j to zero and crank it back up to n-1 in the for loop.

    Rethink your logic.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Code:
         j=100;
        
        }
         for(j<=100;i>j;++i){
         printf("%d", array[n]);
        }
      
       for(j=0;j<n;++j){
    like this?, sorry im a beginner, and whatever your saying sounds chinese to me literally. I'm trying HARD!

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Sorry, that was intended to read - This is what you're doing...

    Quote Originally Posted by Tclausex View Post
    You set j to 100 or less, then ask if unknown value i is greater than j, increment unknown value of i, then set j to zero and crank it back up to n-1 in the for loop.

    Rethink your logic.
    Think about your logic.

    First, you never set i to any value, so how can you compare it to j.

    And IF i was greater than j to begin with, you keep incrementing it, so i stays greater than j, so once in the while loop, it will loop indefinitely.

    Code:
    for( j <= 100 ; ... ; ... )
    The first expression in a for loop is meant to be an initializer that executes only at the onset of the for loop, like j = 0. You are just comparing j to 100 now which DOES nothing.
    Last edited by Tclausex; 11-14-2011 at 06:59 PM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Code:
    }
         for(i=0;i>j;++i){
         printf("%d", array[n]);
        }
      
       sum1=sum+array[n];
       scanf("%d", &sum1);
       printf("The sum is %d\n", sum1);
    I feel ddumb! .. umm? I got the looping done atlassssst, but it keeps giving me sum for each integer I input
    Last edited by neveragn; 11-14-2011 at 07:39 PM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    anyone? help?

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Fix your indention and don't mix spaces and tabs. Then check that you're using array[n] or array[i] or array[j] in the right places.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    1. rename variable j to something like max_index
    2. don't use literal 100 when you have SIZE defined

    Code:
    }
    
         for(i=0;i>j;++i){    //When will i > j (max_index) ever be true?  Think about it.
    
         printf("%d", array[n]); // stepping through with i but using n for index?  Use i.
    
        }
    
       
    
       sum1=sum+array[n];  // again use i, and get this up in that for loop.
    
       scanf("%d", &sum1);  // errant scanf() ?
    
       printf("The sum is %d\n", sum1);

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Code:
        {
              max_index=n;
              break;
             
        
           max_index=100;
        
        }
         for(i=0;i<max_index;++i){
          printf("%d", array[i]);
          sum1=sum+array[i];
      
        }
      
      printf("The sum is %d\n", sum1);
           
     }
     return (0);
    Works, but errors are just not ending $$
    Last edited by neveragn; 11-18-2011 at 10:36 PM.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Now, I actually forgot what/how Im suppose to do -_-

  15. #15
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Hard to help without knowing what the errors are.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  2. Replies: 1
    Last Post: 04-25-2006, 01:14 AM
  3. array dimension
    By julianluthor in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2003, 05:01 PM
  4. Two dimension Array
    By jimiexe in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 04:22 PM
  5. 4 dimension array
    By Supar in forum C++ Programming
    Replies: 11
    Last Post: 12-30-2002, 04:29 PM

Tags for this Thread


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