Line 21 overrides any effect line 17 has. Line 24 and 25 don't make sense.
for(j<=100;i>j;++i){
printf("%d", array[n]);
}
The loop counter is i, but you are displaying the index n, isn't in the loop and doesn't vary. The initial part of the loop is "j <= 100". I'm not sure what you're trying to do there. The test is "i > j", but i is not initialized, so it's undefined behavior. You have too many loop counter variable and you are mixing them. Get rid of j altogether. You don't need it. i and n are plenty.
for(i = 0; i < n; i++)
{
printf("%d", array[i]);
}
I imagine that's what you want in lines 24 to 26. Lines 28 to 30 look close, but there are problems. You can keep j if you want, or you can replace j with i. Agan, there's no need for a second loop variable since you have no nested loops.
For-loops look like this.
for(i = ?; i < ?; i++)
{
// Do something with i here
}
Since you have n elements and you want to loop through them, it's what I have above. Having a "<=" operator before the first semicolon is perfectly legal, but it isn't what you want here. You usually want an "=" operator there, as above.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
Here's your code in a more readable format.
#include<stdio.h>
#include<string.h>
int main(void)
{
int array[100], i, n, max_index, sum=0, sum1;
printf("Enter some integers:");
for (n=0;n<100;++n)
{
scanf("%d", &array[n]);
if(array[n] == -1)
{
max_index=n;
break;
max_index=100;
}
for(i=0;i<n;++i)
{
printf("%d", array[i]);
sum1=sum+array[i];
}
printf("The sum is %d\n", sum1);
}
return (0);
}
Line 18 cannot be reached under any circumstances, but that's irrelevant because you never do anything with max_index, so why does it exist?
Lines 20 - 26 -- These lines execute BEFORE the user has finished entering the numbers. Do you want them to? If not, move them AFTER line 26.
Line 17 -- Once I break, I go immediately to the end of the program on line 28. This problem goes hand in hand with the other problem. Your summation loop is in the wrong spot.
Why do you have two summation variables (sum and sum1)? And sum never changes so it's not a variable really.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
Here's your task in generic terms.
- Read in all the numbers.
- Figure out how many numbers you read in.
- Find the sum of those numbers.
- Print it.
That's the order of the steps. The code must follow those steps. Let's pick some descriptive names. Change them if you like.
CAPACITY -- a constant. The size of the array and the maximum number of entries the user can enter.
num_entries -- The ACTUAL number of entries.
sum -- the sum of the actual number of entries.
i -- A generic loop counter variable.
Now let's look at your code and do a little substitution. I'll give you a skeleton and we'll keep what you have correctly.
#include<stdio.h>
#include<string.h>
#define CAPACITY 100
int main(void)
{
int array[CAPACITY];
int i;
int num_entries = 0;
int sum = 0;
// Step 1 from above
printf("Enter some integers:");
for (num_entries = 0;num_entries <CAPACITY; ++num_entries)
{
scanf("%d", &array[num_entries]);
if(array[num_entries] == -1)
{
break;
}
}
// Step 2 from above
// at this point, num_entries should be accurate.
// Step 3 from above
// what goes where the ? is below?
for(i=0; i < ? ; ++i)
{
printf("%d", array[i]);
sum =sum + array[i];
}
// step 4 from above
printf("The sum is %d\n", sum1);
return (0);
}
You pretty much had the whole thing, just a few errors and some incorrect ordering. I left a question mark for you to fill in.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
Line 19 -- no need to check that num_entries isn't greater than 99. Line 15 should handle that.
"There are 7 odd numbersn" isn't anywhere in your code. Can't see how it could print that.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
>> but the outcome is the same
The outcome is "There are 7 odd numbersn"?
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
The output looks spot-on to me. The only thing that looks "weird" is that all your entries run into each other because you don't separate them with spaces, tabs, or newlines. Change line 28 to this and test to make sure everything looks OK.
printf("%d\n", array[i]);
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
Question Answered as of 1 Year Ago by
VernonDozier