I'm new to C and wrote a code where I would like to display "?" on the screen where it matches the value in an array. If I have an array initialized with index_location[6] = {0, 1, 5, 8, 9, 12}; my expected output is detailed below. Any help or assistance in helping me is greatly appreciated.
Output: ??@@@?@@??@@?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//
int main(void)
{
int index_location[6] = {0, 1, 5, 8, 9, 12};
int len=sizeof(index_location)/sizeof(int);
for (int a = 0; a < len; a++)
{
for(int b = 0; b < len; b++)
{
if ( a == index_location[b])
{
printf("%c",'?');
} else {
printf("%c",'@');
}
}
}
printf("\n");
}
for (int a = 0; a < len; a++)as the idiomatic loop; with<=rather than<you access a non-existent element of the array, which leads to 'undefined behaviour', which is to be avoided at all costs (because anything can happen when you invoke undefined behaviour, and anything that happens is 'correct' behaviour).for (a=0; a<len; ++a). Also, you really shouldn't specify both a size and an initializer for index_location:int index_location[]=...would be better.