Question

Jojo is going to a restaurant. There are N foods listed in the menu, and the items are sorted increasingly based on its price. Now Jojo is wondering how many foods are there with price P. As the number of food in the menu can be a lot, Jojo will need your help to answer his questions. Jojo knows you can count really fast, so he will give you M questions. Format Input Input begins with integer N and M, the number of food in the menu, and the number of query. The next line will consist of N integers, the prices of food in the menu sorted increasingly. Then followed by M lines, where each line consist an integer, the price of i-th query. Format Output The output consists of M lines, where each line is the answer to the i-th query, the number of food with price Constraints 1N100 000 1 M100 000 1 <x1 000 000 000 Sample Input (standard input) 4 3 1 2 2 3 Sample Output (standard output) 1. 2. (o 2 ts

C language

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note The code is written as per the requirements The code is compiled and executed in the online compiler codechef and outp//Read input scanf( %d, &priceArray[index]); //Loop to iterate through m values for (int indexl - 0; indexl<mValue; index1++) //Declare a variable for query int query; //Declare a variable for counter int counter; //Read input scanf( %d, &query); //Call searchArray) method countersearchArray (query, nValue, priceArray); //Display result printf (%d , counter); Sample Output Status Successfully executed Date 2019-01-24 09:50:17 Time 0 sec Mem 9.888 kB Input 1 2 23 2 Output

Copyable code

//Include the needed header
#include<stdio.h>

//Method searchArray()
int searchArray(int query, int nValue, int priceArray[])
{
   //Declare and initialize counter
   int counter = 0;

   //Loop to iterate through n values
   for (int index = 0; index<nValue; index++)
   {
       //Check condition
       if (priceArray[index] == query)

           //Increment counter
           counter++;
   }

   //Return counter
   return counter;
}

//Method main()
int main()
{
   //Declare a variable for n
   int nValue;

   //Declare a variable for m
   int mValue;

   //Read input values
   scanf("%d %d", &nValue, &mValue);

   //Array to hold food prices
   int priceArray[150000];

   //Loop to iterate through n values
   for (int index = 0; index<nValue; index++)
   {
       //Read input
       scanf(" %d", &priceArray[index]);
   }

   //Loop to iterate through m values
   for (int index1 = 0; index1<mValue; index1++)
   {
       //Declare a variable for query
       int query;

       //Declare a variable for counter
       int counter;

       //Read input
       scanf(" %d", &query);

       //Call searchArray() method
       counter = searchArray(query, nValue, priceArray);

       //Display result
       printf("%d ", counter);
   }
}

Add a comment
Know the answer?
Add Answer to:
C language Thank you Jojo is going to a restaurant. There are N foods listed in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C language . Thank you Count Sheep Jojo is having problem to sleep at...

    Write in C language . Thank you Count Sheep Jojo is having problem to sleep at night. He can't fall asleep and it makes him feel tired every day. Having this problem, Jojo told his friend Bibi, and Bibi advised him to do sheep counting while he tries to sleep. Jojo decided to try using this trick for N nights, to test its effectiveness. Jojo realized that he would fall asleep if he imagined a total of 10 white sheep....

  • Write in C language Diamond Jojo want to propose to Lili. Jojo think he should give...

    Write in C language Diamond Jojo want to propose to Lili. Jojo think he should give Lili a diamond when he proposed to her. But Jojo is having problem to choose the diamond so he asked you to show him diamonds based on the size he told you Format Input The input consists of only one integer S, denotes the size of the diamond. Format Output Show the diamond Jojo want to see, Constraints 4 SS S 30 Page 1...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • write in C language Decoration Lights Jojo is currently working in an office as a security....

    write in C language Decoration Lights Jojo is currently working in an office as a security. Every night, after everyone returned home, he needs to make sure all decoration lights in the office is turned off. The decoration lights are unique: each of them has a timer that will switch the light on or off every two seconds. The lights are also arranged so well that each two neighboring lights will have different state (on/off). As long as the timer...

  • C Programming Language The code below matches the sample input/output but is marked wrong. Are there...

    C Programming Language The code below matches the sample input/output but is marked wrong. Are there other ways to do this problem? The focus is on recursion and functions. #include<stdio.h> int joCheck(unsigned long long int number) { if(number % 7 == 0 || number % 8 == 0) { return 1; } else return 0; } int main() { int cases = 0; scanf("%d", &cases); for(int i = 1; i<=cases; i++) { unsigned long long int number; scanf("%d", &number); if(joCheck(number)...

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...

    C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every time Bibi climbs a set of stairs, she counts the steps starting from 1 to the number of steps in that particular set of stairs while climbing the stairs. For example if she climbs two set of stairs, the first containing 5 steps and the second containing 3 steps, she will say 1, 2, 3, 4, 5, 1, 2, 3 and the total number...

  • Use c language Project Euler #5: Smallest multiple H HackerRank This problem is a programming version...

    Use c language Project Euler #5: Smallest multiple H HackerRank This problem is a programming version ofProblem 5 from projecteuler.net 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? Input Format First line contains T that denotes the number of test cases. This is followed by...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located...

    C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located in Nusa Tenggara Timur (NTT), is a World Heritage Site by UNESCO, the specialized agency under United Nations (UN). One day, Lili and Bibi were on vacation on one of the islands in the Komodo National Park area and they just realized that they were living alone on that island because the cot- tage owner was completing his unfinished business on the main island....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT