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: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his...

    C Programming Language Problem Title: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his family. Suddenly an instant message came in. Apparently there is a message from Lili : ”Jojo, don’t forget to check the forum. Who knows if there is an assignment before the exam” Jojo finally took the time to check the discussion forum and sure enough, there were some assignments given to his class. He immediately told his classmates. Jojo finds out one difficult problem...

  • 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)...

  • C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is...

    C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is currently pursuing his Computer Science degree. In one of his Computer Vision lectures, his lecturer tackled a topic about binary thresholding. For simplicity, binary thresholding is an image filtering method used to separate low val- ued pixels (dark) from high valued pixels (bright). Each pixels in an image is classified as low or high based on a certain threshold K. If a pixel Pij...

  • C Programming Language Problem Title: Introduction to Database Database is a structurized method for data storage...

    C Programming Language Problem Title: Introduction to Database Database is a structurized method for data storage and perform operations related to the data. There are Some simple operations that can be done by a database are Insert, Alter, Delete, and Query. There are N different integers stored. You are asked to create a database where you can carry out the following 4 operations : 1. I X - this operation is insert operation, inserting X to the database. 2. A...

  • 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...

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