VortexX Bolt
VortexX Bolt

Reputation: 15

How Do I Make This Print?

I understand how to print the stuff now: I forgot to allocate memory for the array. However, now I can't seem to get the math right in the final result.

Prompt:

A boy goes to buy video games from a shop. The shop contains N unique video games. The prices of the games are given in the form of an array A. The price of ith games is A[i]. Now the boy has q queries, in each query he wants to know the number of unique games that have a price less than the given amount M. Input:

The first line contains an integer N total number of unique video games available in the shop.

The second line contains N space-separated integers (the price of the games).

The third line contains Q number of queries.

Each of the next Q lines contains integer M. Output:

For each query output number of games having price less than M for that query. Sample Input:

5
1 4 10 5 6
4
2
3
5
11

Output for the sample input:

1
1
2
5

Code:

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

int main() {
int *Prices, UniqueGames, j, h, i, numPrice, numQueries, *PriceQueries, *TestResults;

    //Making Arrays
    printf("Enter number of unique games.");
    scanf("%d", &UniqueGames); // Array Size

    Prices = (int*)malloc(sizeof(int)*UniqueGames); // Memory Allocation

    for(j = 0; j < UniqueGames; j++){ // Filling Array
        scanf("%d", &h);
        *(Prices+j) = h;
    }

    printf("Enter number of queries.");
    scanf("%d", &numQueries); // Array Size

    PriceQueries = (int*)malloc(sizeof(int)*numQueries); // Memory Allocation

    for(j = 0; j < numQueries; j++){ // Filling Array
        scanf("%d", &h);
        *(PriceQueries+j) = h;
    }

    //Calculations
    TestResults = (int*)malloc(sizeof(int)*numQueries);
    h = 0;
    for(j = 0; j < numQueries; j++){ // Filling array TestResults with test results
        for (i = 0; i < UniqueGames; i++) {
            if (Prices[i] < PriceQueries[i]) {
                h = h + 1;
                TestResults[j] = h;
        }
            else {
                h = 0;
            }

        }
        printf("%d\n", TestResults[j]);

    }

    free(Prices);
    free(PriceQueries);
    free(TestResults);
   return 0;
}

I'm filling an array of prices for each game and an array of prices for the number of queries the user inputs. I fill an array of test results based on the prices within the query set and I'm trying to print that final array.

Upvotes: 0

Views: 106

Answers (1)

Fe2O3
Fe2O3

Reputation: 8344

Your 'calculations' are needless complex making it difficult to understand. (And the wonky indentation does not help.)

//Calculations
TestResults = (int*)malloc(sizeof(int)*numQueries); // <== uninitialised array
h = 0;
for(j = 0; j < numQueries; j++){ // Filling array TestResults with test results
    for (i = 0; i < UniqueGames; i++) {
        if (Prices[i] < PriceQueries[i]) { // <= one index should be 'j'
            h = h + 1;
            TestResults[j] = h; // <== assignment should be accumulation
    }
        else {
            h = 0; // <== why reset???
        }

    }
    printf("%d\n", TestResults[j]); // <== why use an array???

}

Rewriting (with comments) may help clarify what you seem to want to achieve

//Calculations
for( j = 0; j < numQueries; j++ ) { // for each query amount...
    int cnt = 0; // count the games...
    for( i = 0; i < UniqueGames; i++ ) // for each game
        if( Prices[ i ] < PriceQueries[ j ] ) // priced less than this query amount
            cnt++;

    printf( "Query %d: %d games cost less than %d\n", j, cnt, PriceQueries[ j ] );
}

Upvotes: 1

Related Questions