Help with code using arrays

closed account (SN0i1hU5)
Hey everyone! i'm new here and i need help with this assignment and my code.
What i need help on in particular is the "string store[10]".

"Write a program that will prompt for and read in a list of up to ten computer store names and the corresponding price that each store charges for a particular model of PC, and will then determine the lowest price charged for that PC among the stores input. Then, the program should print two lists:
List 1 – The names of all stores charging the lowest price.
List 2 – The names and prices for all stores whose price does not exceed the lowest price by more than 10%.
Hint: Use two arrays - one to store the names and the other to store the prices."

Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

const int NUMBER_OF_STUDENTS = 10;

using namespace std;

int main()
{
	string store[10];
	
	int i, price[NUMBER_OF_STUDENTS], min;
	
	cout << "Enter 10 prices: " <<endl;
	cin >> price[0];
	min = price[0];
	
	for (i = 1; i < NUMBER_OF_STUDENTS; i++)
	{
		cin >> price[i];
		if (price[i] < min)
			min = price[i];
		//min is the lowest of the values price[0] .... price[i].
	}
	
	cout << "The lowest price is " << min <<endl;
	
	return 0;
}
Last edited on
What i need help on in particular is the "string store[10]".


That line of code tells that compiler to create an array of 10 strings in memory that are "named" store[0], store[1], store[2], ... store[9]. (Well, technically, the individual strings do not have names, but that is how you access them.)

A question for you is, what is NUMBER_OF_STUDENTS? Nothing with that meaning shows up in the problem description. Maybe you should change that to NUMBER_OF_STORES, and change the line in question to string store[NUMBER_OF_STORES];

Oh. Please go back and edit your post. Highlight the code and click the Format button that looks like "<>". This will insert code tags into your post to make it easier to read and comment on.
closed account (SN0i1hU5)
NUMBER_OF_STUDENTS is meant for int price[10] array not for string array. what i need to know is how to store whatever words or names the user inputs and have those entries be placed into for string store[10] and somehow print whichever one is connect to each entry for price[i]. price[NUMBER_OF_STUDENTS] is for price[10] array and that picks out whichever input is the lowest number among the ten numbers the user entered. basically how to do
List 2 – The names and prices for all stores whose price does not exceed the lowest price by more than 10%.
Last edited on
NUMBER_OF_STUDENTS is meant for int price[10] array not for string array


Why not? Each store has a name and a price. 10 stores -> 10 names, 10 prices. You need to index the arrays by the store number.

But let's step back a second.

If you were only looking for the lowest price, you could just read each name and price and save off the lowest price and the accompanying name. But, you might need to have multiple names at the same lowest price. And additionally, you need to know all of the places that are within 10% of the lowest price. So, you need to store ALL of the names and prices that are inputted before you can determine which are going to be in list 2.

That's why your teacher recommended 2 arrays. One array will contain the name of each store, and the other array will contain the price at each store. The name needs to correspond to the price so when you find prices that fit into a list, you can determine the store name associated with it.

So, both the name and price arrays should be indexed by store number. There is no "NUMBER_OF_STUDENTS" in the problem description.

You should write your program in stages. In the first stage, merely read in the 10 store names and prices and print them back out. Make sure you can read into an array and access the array elements properly (hint - use for loops).

Next, figure out what the lowest price is and print it out.

Next, print out the names of the stores that have the lowest price.

Next ... you should have the picture by now.
closed account (SN0i1hU5)
The name needs to correspond to the price so when you find prices that fit into a list, you can determine the store name associated with it.


can you show me an example of this. i'm not a verbal learner, i'm more of a visual learner.
Try these web pages.

http://mathbits.com/MathBits/CompSci/Arrays/Parallel.htm
https://en.wikipedia.org/wiki/Parallel_array

If these don't clear things up, try googling "parallel arrays" and see if some of those pages clear anything up.

If you are still confused, at least then you might have a better idea about what your are confused about and can ask a more directed question.
Topic archived. No new replies allowed.