# Thread: newbie assignment help

1. ## newbie assignment help

hey im working on an assignment and am not sure where i went wrong.
Code:
```#include <stdio.h>
int main()
{
int data[99];
int val;
int i;

for(i=0;i<=99;i++)
{
printf("Enter an integer value (0 to quit): ");
scanf("&#37;d", &val);

if(val==0 || val==data[99])
{
printf("Values in reverse order: \n%d\n", data[i]);
}

}

return 0;
}```
the objective is to accept some numbers and then if the array is full or if "0" is entered, the numbers will print in reverse order (where i encountered problem). also, after it displays "numbers in reverse" it restarts the loop..how can i get out also?

2. Code:
```/* arraylist.c */

#include <stdio.h>
int main()
{
int data[99];
int val;
int i;

for(i=0;i<=99;i++)
{
printf("Enter an integer value (0 to quit): ");
scanf("%d", &val);

if(val == 0 || i>99)
{
data[i] = val;

}

}

/* add your reverse printout here */

return 0;
}```
also look in to using while() for your scan loop.

3. Well you do have a lot more closing braces than opening braces.

> for(i=0;i<=99;i++)
This steps off the end of the array. Use
for(i=0;i<99;i++)

Printing in reverse just counts backwards from whatever 'i' you got to.

4. Think about what you would do on paper. Let's say you have 2, 8, 7, 1, and 6 as your numbers. In reverse order, you would have 6, 1, 7, 8, then 2. Note how the swapping is done. Do the same for your system. One other problem is that you are accessing an array index value (the number in the brackets) outside the allotted range. "number[8] being defined doesn't mean you can access "number[8]" in the code (unless you somehow change the array size, but even then, it's always going to be one less than the value you set as the highest you can use when accessing it). Look into resolving these then look at how to code it properly.

5. Code:
```          int arr[20];
int i;

for (i = 0; i < 20; i++) {
scanf("%d", &arr[i]);

if (arr[i] == 0)
break;
}

if (i == 20)
--i;

/* reverse */
do {
printf("%d ", arr[i]);
} while (i-- > 0);

return 0;
}```

6. hmm so i went back and fixed my code and got something like krpytkat's. however, when i get to the second print part, i get stuck and im not really sure why.

thanks for all the help.
Code:
```	for(j=data[i];j>=j[0];j--)
{
printf("The Reverse order is: \n&#37;d", data[j]);
}```

7. Code:
```#include <stdio.h>
int main()
{
int MaxNum = 5;
int data[MaxNum];
int val;
int i;

for (i=0; i<MaxNum; i++)
{
printf("Enter an integer value (0 to quit): ");
scanf("&#37;d", &val);

if (val==0 || i == MaxNum - 1)
{
printf("Values in reverse order: \n");
for (; i >= 0; i--)
printf("%7d ", data[i]);
}
if (i < 0) i = 0;
if (val == 0) break;
}

return 0;
}```
the objective is to accept some numbers and then if the array is full or if "0" is entered, the numbers will print in reverse order (where i encountered problem). also, after it displays "numbers in reverse" it restarts the loop..how can i get out also?
Your "new" code offered nothing I could make sense of, sorry. I haven't run the code above, so test it out for yourself - it may have simple errors in it, still.

Anything you don't understand, just let me know.

8. Code:
```	for(j=data[i];j>=j[0];j--)
{
printf("The Reverse order is: \n%d", data[j]);
}```
after you exit the first for() loop your variable is out of scope. Meaning you can not do data[i] in the intializer it has to be
Code:
`    For(j=0 ; j>0 ; I++ )`
edit note should be j++ not i++

or a variable like this
Code:
`for (i=0; i<MaxNum; I++)`
if you get stuck write your prog on paper then trace each step and print the output manually. So you know what is going on with your prog. Or write a sutocode like so .... so you know what you want to do....

Code:
``` int main

for() .... get in data with scanf() and store data in array

for() .... print array in reverse order

exit or return 0;```

Popular pages Recent additions