acm1pt2
acm1pt2

Reputation: 29

Using Arrays in C (to print an input with another input)

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)

Upvotes: 0

Views: 44

Answers (1)

knightrider
knightrider

Reputation: 2153

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]);
    }
}

Upvotes: 2

Related Questions