Reputation:
I started my computer science course in college, and we're studying arrays. We have to write a program that reads a sequence of data, and prints it in reverse order. The char '%' declares the end of the sequence.
This is the program that I wrote:
#include <stdio.h>
int index;
int x;
int sequence[10];
int main () {
index = 0;
printf("Insert x: ");
scanf("%d", &x);
while (x != '%') {
sequence[index] = x;
printf("Insert x: ");
scanf("%d", &x);
index = index + 1;
}
while (index > 0) {
index = index - 1;
printf("%d", sequence[index]);
}
}
It reads the sequence via printf, saves each number in an array and when it receives a '%', it starts printing the sequence in reverse order, with the second while iteration.
The main problem is: when I try to run it, I get an error after that I enter '%', it starts printing back "Insert x: " lots of time, and then it crashes.
The second problem is: do I need to declare the size of the array?
Thank you.
Upvotes: 2
Views: 169
Reputation: 12708
The reason is that scanf(3) returns the number of items read with that format string "%d"
, and as you don't check that number, it's getting one until you introduce the %
(which is not a valid number), scanf(3) returns 0
(because it cannot convert that thing to an integer) and doesn't advance the file pointer from this point on, reading the %
again and again. Each time, the last read value is getting assigned to one more cell of the array, overflowing it past of its limit (the last element is 9 but you allow to go up to element 10, one more), beggining to write other memory and probably crashing the program.
change the while loop to read so:
while ((scanf("%d", &x) == 1) && (index < 10)) { ...
Also, use <
as the array is an array of 10 elements, numbered from 0 to 9.
Upvotes: 0
Reputation: 8308
This will scan an int. If it is successful the int will be added to the array until 10 ints have been added. If it fails to read an int, getchar will read a char and if the char is %
the main loop will exit. If the char is not %
it will try to read another int.
The size of the array needs to be declared.
#include<stdio.h>
#include<stdlib.h>
int main() {
int x = 0;
int index = 0;
int sequence[10] = {0};
int ch = 0;
printf ( "Input 10 integers (% to exit)\n");
while ( ch != '%') { // loop until ch is a %
if ( scanf ( "%d", &x) == 1) { // scanf read an int
sequence[index] = x;
index++;
if ( index >= 10) {
break; // too many inputs for array
}
}
else {
ch = getchar(); //scanf failed to read int. read a char and retry
}
}
while (index > 0) {
index = index - 1;
printf("%d\n", sequence[index]);
}
return 0;
}
Upvotes: 4
Reputation: 4010
As per your program. You are checking a character with integer value in while loop. Either you change your code like below or change condition in while loop as X!=? where ? is a ascii value of '%'. you can increase the array size but array value must be declared
#include <stdio.h>
int index;
char x;
char sequence[10]; //you can increase the array size here
int main () {
index = 0;
printf("Insert x: ");
scanf("%c", &x);
while (x != '%' && index <= 10) {
sequence[index] = x;
printf("Insert x: ");
scanf("%c", &x);
index = index + 1;
}
while (index > 0) {
index = index - 1;
printf("%c", sequence[index]);
}
return 0;
}
Upvotes: 0
Reputation: 111
It crashes because inside the loop there are no condition to stop when you reach array max size. At certain time : index = 10 -> sequence[10] doesn't exist because in c index must go from 0 to (array size - 1).
modify this while (x != '%') { to this while ((x != '%') && (index <= 10)) {
For your question about array size declaration, the answer is YES, the size of an array must be always declared.
Upvotes: 0