08-18-2009 12:42 PM
// I having trouble converting my c++ program to c.
//NON-FATAL RUN-TIME ERROR: "test c files.c", line 24, col 13, thread id 0x000013E8: Array index (17675088) too large (maximum: 5).
//NON-FATAL RUN-TIME ERROR: "test c files.c", line 24, col 13, thread id 0x000013E8: Array index (17675088) too large (maximum: 5).
#include <ansi_c.h>
#include <utility.h>
#include <stdio.h>
void binary(int);
void main(void) {
int number;
printf("Please enter a positive integer: ");
scanf("d%",&number);
if (number < 0)
printf("That is not a positive integer.\\n");
else {
printf("%d",number," converted to bits is: ");
binary(number);
//return 0;
}
}
void binary(int number) {
char temp[][10]={"00000000","00000001","00000010","00000100","00001000","0001000"};
printf("%d",temp[number]);
GetKey();
}
08-18-2009 01:24 PM
I suspect that since your declaration of the temp array is incomplete (first dimension omitted) it is established by the number of items in the initializer (Six ten-character array elements, zero based, 0 -> 5).
So if user asks for a number greater than 5 and you try to index into the array with it you'll have walked off the end of the temp array and you get a runtime error.
08-18-2009 01:51 PM
//this is the changes, no errors but if i enter in a number between one through 5 its only printing out the first string
//which is 00000000.
//its suppose to do this :if i enter 1, it prints out 00000001, if i enter 2 ,it prints 00000010.
#include <ansi_c.h>
#include <utility.h>
#include <stdio.h>
void binary(int);
void main(void) {
int number=0;
printf("Please enter a positive integer: ");
scanf("d%",&number);
if (number < 0)
printf("That is not a positive integer.\\n");
else {
printf("%d",number);
binary(number);
//return 0;
}
}
void binary(int number) {
char temp[][10]={"00000000","00000001","00000010","00000100","00001000","0001000"};
printf("%s",temp[number]);
GetKey();
}
08-18-2009 02:47 PM
Darnell:
You have the format string wrong on your scanf statement. The format string should be "%d", not "d%".
Before you correct it, try some basic debug. set a breakpoint after the scanf statement, then look at the value of number. You'll see that it is 0.
Basic debugging here starts by evaluating what your output is, and then determining where it came from. If you set a breakpoint in your binary function, you would see that number was always 0, which is why you always returned "00000000". If you would have looked at your output, you would have seen that your number was 0 as well.
Please don't continue to post the same question to multiple threads. You posted this same question on c++ conversion to your thread on pass/fail test.
08-19-2009 09:08 AM
The "d%" does seem to be the problem here. In addition, in your next line with the printf() you probably just want a "\n" instead of your double backslash "\\n". Also note, this isn't really converting C++ to C as it is basic debugging of your code. As AI S suggested, use your compiler and debugging tools to your advantage. If you need some help getting started with debugging here's a short concise tutorial with lots of techniques
Regards,
Steven Zittrower
Applications Engineer
National Instruments