# Thread: One-dimension array SUM prolem

1. ## 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. 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. ohh yeah how did I forget that!

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

4. 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. 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);
}```

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

7. 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. Sorry, that was intended to read - This is what you're doing...

Originally Posted by Tclausex
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.

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.

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

10. anyone? help?

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

12. 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. 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 \$\$

14. Now, I actually forgot what/how Im suppose to do -_-

15. Hard to help without knowing what the errors are.