1,105,625 Community Members

finding the sum of an array

Member Avatar
hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

the basic idea of what im trying to do is write a program that reads an arbitrary number of integers less than 20. then it prints out the position of the integer in the order the user inputed it then it finds the sum when it scans a non integer character like *. so it would look something like this if it run correctly

36 27 55 -3 42 66 -5 *

Entered integer number 0: was 36
Entered integer number 1: was 27
Entered integer number 2: was 55
Entered integer number 3: was -3
Entered integer number 4: was 42
Entered integer number 5: was 66
Entered integer number 6: was -5
The sum is: 218

i have written this so far

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }

  int n = SIZE;
  for (i = 0; i < n;){
    sum = sum +  array[i];
    printf( "The sum is: %d\n", sum);      
  }
  return 0;
}

it pretty much does the first part of reading out the position of the integers but it goes nuts when its time to find the sum. any help would be appreciated

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

You forgot the i++ in the first line of the for loop.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Define "goes nuts". That is hard to imagine.

Explaining a problem correctly often results in more accurate answers and better help.

Member Avatar
Ancient Dragon
Achieved Level 70
27,639 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

>>for (i = 0; i < n;++i){

What will happen if you only input 3 numbers? Answer: the final value of sum will be in the crapper because it will add in uninitialized array elements 4-20.

Member Avatar
hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

the basic idea of what im trying to do is write a program that reads an arbitrary number of integers less than 20. then it prints out the position of the integer in the order the user inputed it then it finds the sum when it scans a non integer character like *. so it would look something like this if it run correctly

36 27 55 -3 42 66 -5 *

Entered integer number 0: was 36
Entered integer number 1: was 27
Entered integer number 2: was 55
Entered integer number 3: was -3
Entered integer number 4: was 42
Entered integer number 5: was 66
Entered integer number 6: was -5
The sum is: 218

i have written this so far

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }

  int n = SIZE;
  for (i = 0; i < n;){
    sum = sum +  array[i];
    printf( "The sum is: %d\n", sum);      
  }
  return 0;
}

it pretty much does the first part of reading out the position of the integers but it goes nuts when its time to find the sum. any help would be appreciated

this is the new code i have now

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }  
 
  for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum);
  }      
      
  return 0;
}

it doesnt find the sum. when you input 3 numbers like
1 2 3 *
it just does this

Entered integer number 0 was: 1
Entered integer number 1 was: 2
Entered integer number 2 was: 3
The sum is 1

then the program ends.
am i doing anything wrong? because the sum definitely isnt one

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

You have to work out the simplest, clearest, and most efficient way to program.

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    sum += array[i];  //<----- add this line
    i++;
  }  
 
/* Right here, i equals the number of int's you have put into the array, so
the for loop below could be simplified n=0;n<=i;n++. However it shows an
even simpler and more efficient answer - put the sum into the same while()
loop as the entry (why the hell not?), and forget this second loop,
completely - it's useless. 
*/ 


//Ditch this loop like a bad habit :(
 for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum); //??? you're joking right?
  }      


  printf("The sum was %d", sum);      
  return 0;
}
Member Avatar
hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You have to work out the simplest, clearest, and most efficient way to program.

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    sum += array[i];  //<----- add this line
    i++;
  }  
 
/* Right here, i equals the number of int's you have put into the array, so
the for loop below could be simplified n=0;n<=i;n++. However it shows an
even simpler and more efficient answer - put the sum into the same while()
loop as the entry (why the hell not?), and forget this second loop,
completely - it's useless. 
*/ 


//Ditch this loop like a bad habit :(
 for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum); //??? you're joking right?
  }      


  printf("The sum was %d", sum);      
  return 0;
}

thank you for the help. one question though, if i wanted this program to find the sum like it does in this case when its sees a non int character but not terminate, how would i do that? i mean i want it to give you the sum when its sees a character but still allow you to put more numbers in. would i have to place this program inside another loop?

Member Avatar
hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you for the help. one question though, if i wanted this program to find the sum like it does in this case when its sees a non int character but not terminate, how would i do that? i mean i want it to give you the sum when its sees a character but still allow you to put more numbers in. would i have to place this program inside another loop?

ignore that quest

Question Answered as of 3 Years Ago by Adak, Ancient Dragon and WaltP
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: