0

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");
}
4
  • 1
    You should really show what you actually get. I suspect it is a lot more data than your expected output. Are you sure the double loop is needed? You should be using a single loop, I think. In C, you need to use 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). Commented Jul 11, 2016 at 1:38
  • Should it not be printf('?') according to your requiermets Commented Jul 11, 2016 at 1:40
  • Array boundaries are off 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. Commented Jul 11, 2016 at 1:42
  • 2
    Ok, now you have changed the code on us making all the comments irrelevent. But still haven't told us what the actual behaviour of your program is. And even your requirements are not clear: "where it matches the value in an array". What is it and what is it matching with? Commented Jul 11, 2016 at 1:45

2 Answers 2

3

You only need a single loop. You need to go through index positions 0..12 inclusive (where 12 is the last entry in the index_location array), checking whether you need to print a ? or @ each time. When you've printed the character corresponding to one of the index_location entries, you need to move on to looking at the next one. You could add a liberal collection of assert() invocations to ensure that things are under control, but as long as the array is in sorted order, you should be OK (and you might get away with it even if it's not, though you might miss some requested ? marks).

#include <stdio.h>

int main(void)
{
    int index_location[6] = {0, 1, 5, 8, 9, 12};
    int len = sizeof(index_location) / sizeof(index_location[0]);
    int max = index_location[len - 1];
    int idx = 0;

    for (int a = 0; a <= max; a++)
    {
        if (a < index_location[idx])
            putchar('@');
        else
        {
            putchar('?');
            idx++;
        }
    }
    putchar('\n');
    return 0;
}

Output:

??@@@?@@??@@?
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation.
0

The error lies in your for loops. I would go with the below solution.
If the array index_location[] is sorted in ascending order, use this for loop with double initialization intead:

int max = index_location[len-1];
for(int i = 0,j = 0; i <= max; i++)
{
    if ( i == index_location[j])
    {
       printf("%c",'?');
       j++;
    } 
    else {
       printf("%c",'@');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.