0

I tried to make myself clearer on the question but I don't know how to ask my problem. So I want to create a code in which I input N number of tickets and the N number of winners. Example:

input:

5 3 (Here is 5 and a 3, each one a different input)
382
55
44
451
128
1 
4
3

Output:

382
451
44

So what I have of code is this:

#include <stdio.h>

int main()
{
    int winners;    
    int n;
    int m;
    char ticketWinners[1000][100], ticket[1000];
    int i;
    int j;
    int max[100];

    scanf("%d", &n); //Input for Number of tickets
    scanf("%d", &m); //Input of the ticket numbers(order) that won

    for(i=0;i<n;i++)
    {
        scanf("%s",&ticket[i]);
        {
            for(j=0; j<m;j++)   
            {
                scanf("%s", &ticketWinnersj]);  
            }

            if (j=i);
                printf("%d", winners);
        }
    }
}

The thing is that I don't know how to print the tickets 1, 4 and 3. (I can choose which tickets that won with the input. So instead of 1, 4 and 3; I can choose 3, 5 and 1 respectively)

0

1 Answer 1

2

If I understand your question correctly, the following should work.

In the original code there was a nested loop. I am assuming that this was not the intention.

In the original code the integers were read into a char array. I changed it to int, because it suits the program logic better.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int winners;
    int n;
    int m;
    int ticketWinners[1000], ticket[1000];
    int i;
    int j;
    int max[100];

    scanf("%d", &n); //Input for Number of tickets
    scanf("%d", &m); //Input of the ticket numbers(order) that won

    for(i = 0; i < n; i++)
    {
        scanf("%d", &ticket[i]);
    }

    for(j = 0; j < m; j++)
    {
        scanf("%d", &ticketWinners[j]);
    }

    for(j = 0; j < m; j++)
    {
        // ticketWinners[] has the index of winners. Lets access
        // ticket[] with those indices. Since input index starts 
        // from 1 rather than 0, subtract 1 
        printf("%d \n", ticket[ticketWinners[j] - 1]);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

I think your interpretation of the question is correct (it was a bit hard to decipher at first). But a few nits. Why scanf("%s") and then atoi rather than just scanf("%d") directly? And probably should sanitise the input before using it as an index into ticket in the last for loop.
You would do well to explain what's different in your solution compared to the original. (Now you've edited the code, it makes a lot more sense.) Please note that it is a good idea to ensure that each scanf() operation is successful before using the result.
@kaylum I just fixed it
@JonathanLeffler The original code has nested loops which seems wrong so I removed it. Since they are going to different arrays, I dont see the problem of overwriting
Thanks everyone for helping me out, and yes you got the interpretation right (thankfully :) ) but I tried your code and it prints me random ticket numbers. Thanks again.
|

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.