Parallel Array Help

Hello, I am having some difficulty starting this program. It asks for my program to use two parallel 5 - element arrays: an array of strings that holds the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type.

And this is what I am starting with..

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
     const int SALSA = 5;
     int jars[SALSA];
     char types[SALSA];
}


I am not sure how to input five different strings into the char array, which is giving me quite the headache to figure out. Please help me out. Thanks
perhaps I could use a two-dimensional array?
There is two ways, use string or **char (pointer to pointer) to store strings.
Last edited on
Ok, I figured out the program but I still have a problem displaying the names of the highest and lowest selling products.

Here is my program:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int SALSA = 5;   //number of different types of salsa
	const int STRING_SIZE = 8;   //maximum size of each string
	char types[SALSA][STRING_SIZE] = { " mild" , "medium" , "sweet" ,"hot" , "zesty"};
	int jars[SALSA],
		 highest, lowest,
		 total = 0;

	for(int count = 0; count < SALSA; count++)
	{
		cout << " What is the number of jars sold for " << types[count] << " type salsa? ";
		cin >> jars[count];
		
		while(jars[count] < 0)
		{
			cout << "\n The number of jars sold cannot be less than 0." << endl;
			cout << " What is the number of jars sold? ";
			cin >> jars[count];
		}
		total += jars[count];
	}

	highest = jars[0];
	lowest = jars[0];

	for(int count = 0; count < SALSA; count++)
	{
		cout << "\n " << types[count] << " type salsa sold " << jars[count] << " jars." << endl;
		if(jars[count] > highest)
		{
			highest = jars[count];
		}
		if(jars[count] < lowest)
		{
			lowest = jars[count];
		}
	}
	
	cout << "\n The total number of jars sold was " << total << " jars." << endl;
	cout << "\n The most jars sold was " << highest << " jars." << endl;
	cout << "\n The lowest jars sold was " << lowest << " jars." << endl;
	
	return 0;
}
closed account (D80DSL3A)
Instead of finding the highest and lowest numbers find the indexes to them.
ie. Instead of:
highest = jars[0];
use:
highIndex = 0;
Then instead of:
1
2
3
4
if(jars[count] > highest)
{
	highest = jars[count];
}

Do this:
1
2
3
4
if(jars[count] > jars[highIndex])
{
	highIndex = count;
}

These will also be the indexes to the corresponding types.
Oook I see now, thanks for the help :D
Its really good to see and resolving the problem. Thanks to all guys.
android spy
Last edited on
I have another array problem.

This is my program:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int QUARTER = 4;
	const int DIVISION = 6;
	int company[DIVISION][QUARTER],
		 total, index;
		
	cout << setprecision(2) << fixed;
	cout << " Enter four quarterly sales figures for six divisions of a company." << endl;

	for(int division = 0; division < DIVISION; division++)
	{
		for(int quarter = 0; quarter < QUARTER; quarter++)
		{
			cout << " Division " << (division + 1) << " Quarter " << (quarter + 1) << ":  ";
			cin >> company[division][quarter];
			while(company[division][quarter] < 0)
			{
				cout << " The quarterly sales figures cannot be less than 0.\n";
				cout << " Enter the sales figure again: ";
				cin >> company[division][quarter];
			}
		}
		cout << endl;
	}

	index = 0;

	for(int division = 0; division < DIVISION; division++)
	{
		total = 0;
		for(int quarter = 0; quarter < QUARTER; quarter++)
		{
			total += company[division][quarter];
		}
		cout << " Division " << (division + 1) << "'s sale figure: " << total << endl;
		// Each division's increase or decrease from the previous quarter 
	}

	return 0;
}


I cant figure out how to display each division's increase or decrease from the previous quarter, the first quarter won't be displayed.
Topic archived. No new replies allowed.