• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C/C++ Question Please solve this Q using appropriate data structure.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have attached the Question in the image.
Can someone please solve this Question? I'm a Beginner in Programming.
EDIT:
I tried something like this but I'm unable to figure out whether I can solve this Question using arrays or any other appropriate data structure is required.



Edit 2:
I tried using arrays. I want to set an array for each process K7, K5 and then after top 5 values are inserted in each array wrt to the process id.
But as far as I understand we cannot create an array with each process id name as array name, similarly for stack. So how to store each process id's prices collectively at one place?
So my doubt is how to store 5 prices in one place corresponding it to each process id. How to create a data structure for with process id as name?
Edit 3:

Each product/process id's top 5 entries of prices needs to be stored at one place. Then the entries needs to be sorted at it's entry.
Q1.1.png
[Thumbnail for Q1.1.png]
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch!

It looks like you're asking somebody here to do your homework for you? Well you won't find anyone who will do that for you but if you show us how you're getting on with doing it yourself and have a specific question about it then I've no doubt you'll get the help you need.

The point of homework is to help you learn something for yourself. Don't pass up that golden opportunity.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Marshal

I tried this Code but I'm unable to figure out whether I can use array or any other appropriate data structure can be used to solve this Question.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manaswini potter wrote: . . . I tried this Code . . .

That makes it sound as if you were guessing what to do. You can guess 1000000× and you will be likely to get a few correct solutions. Or you can plan the code and get it right first time. The latter takes much more effort. Please start by explaining what the data structure is to do, not what data structure you want.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Manaswini potter wrote: . . . I tried this Code . . .

Please start by explaining what the data structure is to do, not what data structure you want.


I tried using arrays. I want to set an array for each process K7, K5 and then after top 5 values are inserted in each array wrt to the process id.
But as far as I understand we cannot create an array with each process id name as array name, similarly for stack. So how to store each process id's prices collectively at one place? Like how to take process id I put as array name and create that many arrays.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manaswini potter wrote: . . . I tried using arrays . . ..

That doesn't help you, I am afraid. Your thought process is still on implementation details and that won't enable you to make your decision. Look at this link.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each product/process id's top 5 entries of prices needs to be stored at one place. Then the entries needs to be sorted at it's entry.

(Based on what has to be done(referring to the above link), I hope this time I could answer what has to be done Question)
 
Rancher
Posts: 508
15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly, you want a list within a list. You have a list of products, and each product contains a list of prices; something like that?

So whatever structure you use for your product list items must itself contain a list of product price items. That's fairly easy to implement using arrays if you want to use arrays for your lists (arrays of structures), although it might also be worth finding out about vectors which would probably simplify the code.

Apologies if I've misunderstood.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manaswini potter wrote:Each product/process id's top 5 entries of prices needs to be stored at one place. . . .

Now, that told us what you want to do, and allowed JM to give you a helpful answer.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the Q is each time we enter a product id, like K7 and price as entry 100 and sequence no is 1. Like that we keep enter for different products. But suppose there is a second entry of K7 , 90, 2. Then the K7's list should get updated in sorted order and stored.
Input:
K7,100,1
K7,90,2
K5,80,3
So while printing output:-
K7:100,0,0,0,0.
K7: 90,100,0,0,0.
K5:80,0,0,0,0.

So here the same list K7 gets updated. And on the entry of new list K5, List K5 gets updated.
=>So each time a new product name is entered by user, new separate list must be maintained and if same name is entered previously created older K7 list must be updated.

(So how to keep updating old list and keep creating new list at the same time.)
Note: Sorry for my bad explanation previously. This is what I was trying to tell. Thank you
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you only have one basic operation - update a product's list entry. The only thing that changes is whether you need to create a new list entry first.

1. Read line, and get product ID.
2. Try to find product in list.
3. If product isn't in list, create a new list entry for it.
4. Update product's list entry (which might be the one you have just created).

Does that make sense?
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the same logic from what I've understood I tried coding it,
So first for storing,


And then in the process, In This, For the first value of book type list, I stored the price of it in first element of array. After that I compared the first book product  id with other IDs using if loop if the other product name is same as the first one I inserted in array's next element. So in this way it should only store K7's 100 and 90 but it is also storing K5's 80.


Main Code for Reference:
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't compare the product IDs; it compares the values of the memory addresses where they are stored. You want this:
https://man7.org/linux/man-pages/man3/strcmp.3.html

Also, just use
to read the product ID. That reads the whole ID in one go; no need to do it character by character, which doesn't add the \0 to the end of the string which is required.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But also, I don't think the Process mechanism is correct.

I can see that the Book struct is a record for each line of input, not each product. I think you need a product structure, with only one of those for each product (Book ID). Within the product structure you have the list of 5 prices.

So I think your Load() process is basically ok, just creating a Book record for each line of input. But it's Process() which I think ought to be something like:

1. For each Book in the Book list...
2. Try to find the entry in the Product list with the ID of the Book.
3. If there isn't an entry in the Product list with the Book's ID, create a new one (with the Book's ID and an empty price list).
4. Update the Product entry - add the Book's price to the Product's price list, keeping it in sorted order.
5. Print out the updated Product entry.

Better?
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came up with something like this. I think because I sorted the Product names the prices got mixed up. I want to print the desired output with the code I have written so far. Is it possible? Or Do I have to remove all these vectors and pairs I created which might have complicated the problem? How should I proceed further?



Entry:
k7 100 1
k7 90 2
k7 110 3
k5 80 4
k7 -10 5

Output I got using the above code:
k5 || 100, 90, 110, -10, 15145728, ||
k7 || 0, 0, 15139152, 0, 15145744, ||

Note: sorry for such a messed up code, you wrote the instructions so nicely but while trying to code it I got confused trying to put everything together. Thank you for helping.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sorry for such a messed up code

No probs

Not sure about this:
I think you just want to add the price to the vector, using push_back(). Although the vector then needs (re-)sorting.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and when you output the prices:
Use the size() of the vector instead of 5.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, one more thing:
That gives a vector with one item (0) in it. You want an empty vector - remove the initializer. (Vectors are initialized by default without an initializer, to empty. That is, unlike variables of type int etc. which aren't initialized unless you initialize them.)
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Above this I got Prod vector with unique Product id's and book records the prices each stored in b Book.
Somehow with this  comparison, the prices need to be stored and updated.-how?
2. This seems to do be a bit improper here. Can some other data structure be used instead of vectors? (Other than Vectors and arrays(if possible with this then also ok))
3. I don't know the error but I'm getting

process finished with exit code -1073741819 (0xc0000005)

This error as well. The code after the entries is not working because of
some problem in
This part needs fixing.

And in this:


So basically how to deal with all this above issues?
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry -
I changed that it my version of your code and forgot to mention it. If you read the description of strcmp() in the link I posted, the possible return values are:
  • 0, if the s1 and s2 are equal;
  • a negative value if s1 is less than s2;
  • a positive value if s1 is greater than s2.

  • 1 isn't one of those values. If you want to test for equality, check whether the return value is 0.

    I'll look at the other issues...
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please can you post the whole of the current code?
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would define a 'Product' structure eg.
    I think that's what you should be working with. It could be a vector of those, but you could also try using a map (or unordered_map), with the id value as the key:
    Then you can access the Product records like an array, but using IDs as the array 'index'. You can use
    to check whether a Product with that ID is present in the map. If you know it is present, or to add it to the map, you use
    https://en.cppreference.com/w/cpp/container/map
    or google it.
     
    Manaswini potter
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Current Code:


    Got confused again getting errors.
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, although that doesn't compile.
    https://onlinegdb.com/9HkJ40pS7

    Do you get the same error, or are you working with something slightly different?
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think this (or something similar) is what you want inside your Process() book loop:
    Does that make sense?

    I suggest you make more use of comments, partly to help us understand what you are doing, but mainly to help yourself. I have trouble remembering what I was doing yesterday, never mind weeks/months/years ago
     
    Manaswini potter
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    FINAL WORKING CODE:


    Thanks a lot for all the help, finally understood maps and the question. I thought we have to insert vector inside maps Product format and hence got confused. Yesterday actually I first did with vectors and messed up in that approach, finally after maps approach, it became simpler.
    I'll keep in mind to comment more and explain about what has to be done and what I did as well.
    Thanks a lot.
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just a few suggestions which I think would improve the code.

    For this:
    Define a constructor for Product:
    and make the map a map of pointers to Products:
    Then you can do:
    Although you'd also have to change all (2)
    to

    Also, I know the question seems to want you to use an array, but the Book records you read from the file would be better held as a vector. Keep adding Books to it until you reach the end of the file - check cin.fail() after trying to read the Book's product value. Then use the vector's size() method when you want to know how many records are in the vector.

    The vector of Books (currently array b[]) shouldn't be a global variable. It should  be a local variable in main(), which is either:
  • passed as a reference to Load() for Load() to fill in as it reads from the file, with no (ie. void) return value. Or...
  • Load() could read the records into a local vector, which it then returned to be assigned to the vector in main().

  •  
    John Matthews
    Rancher
    Posts: 508
    15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And I don't know why the Product field in the Book structure is defined in the question as an array. I could understand if this was C, but it isn't - it should be a std::string.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic