Using Parallel Arrays

Dec 13, 2011 at 2:26am
I need help writing a few parts of this program...and I am not sure how to exactly use these arrays.

Alright, here are the instructions for my assignment.

Write a program that lets a maker of chips and salsa keep track of their sales for five different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel five element arrays: an array of strings that hold the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using and initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. Once this sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products.

And here is the code that I have 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Chapter 8 - Assignment 3, Chips and Salsa
// This program produces a sales report for a salsa maker who
// markets 5 types of salsa. It includes total sales for all
// products and identifies the highest and lowest selling product.

#include<iostream>
#include <iomanip>
using namespace std;

// Function prototypes
int getTotal(int [], int);

int posOfSmallest(int [], int);

int main()
{
   const int NUM_TYPES = 5;   
   int sales[NUM_TYPES];
   string name[NUM_TYPES];
   
   // Create the arrays for the names and sales amounts
   
   int totalJarsSold,
       hiSalesProduct,
       loSalesProduct;
     
   // Input the number of jars sold

	for (int type = 0; type < NUM_TYPES; type++)
	{
		cout << "Jar sold last month of " << name[type] << ": ";
      	cin  >> sales[type];
      
		while (sales[type] < 0)
		{	cout << "Jars sold must be 0 or more.  Please re-enter: ";
		   cin  >> sales[type];
		}
	}
   
   // Call functions to get total sales and high and low selling products
   totalJarsSold  = getTotal(sales, NUM_TYPES);
   
   loSalesProduct = posOfSmallest(sales, NUM_TYPES);
   
   // Produce the sales report
   cout << endl << endl;
   cout << "     Salsa Sales Report \n\n";
   cout << "Name              Jars Sold \n";
   cout << "____________________________\n";
   
   for (int salsaType = 0; salsaType < NUM_TYPES; salsaType++)
      // insert the code that prints the salsa names and number jars sold
   
   cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
   cout << "High Seller: "  << name[hiSalesProduct] << endl;
   cout << "Low Seller : " << name[loSalesProduct] << endl;    
        
   return 0;
}

/************************************************************
 *                       	getTotal                         *
 * Calculates and returns the total of the values stored in *
 * the array passed to the function.                        *
 ************************************************************/





/************************************************************
 *                    posOfLargest                          *
 * Finds and returns the subscript of the array position    *
 * holding the largest value in the array passed to the     *
 * function.                                                *
 ************************************************************/






/************************************************************
 *                    posOfSmallest                         *
 * Finds and returns the subscript of the array position    *
 * holding the smallest value in the array passed to the    *
 * function.                                                *
 ************************************************************/
int posOfSmallest(int array[], int numElts)
{
	int indexOfSmallest = 0;
	
	for (int pos = 1; pos < numElts; pos++)
	{
		if (array[pos] < array[indexOfSmallest])
			indexOfSmallest = pos;
	}		
	return indexOfSmallest;
}
Dec 13, 2011 at 2:44am
How much about arrays have you learned? don't be confused by the term "parallel arrays" - that just means (in this case) that there happens to be two of them, and the n'th element of one array is associated in some way with the n'th element of the other array.

(( the reason this association is needed is because arrays can only natively store values of a single type - so you can't, for instance, store a string and a number side-by-side in the same array - each array 'cell' must have the same type as all the others in the array: a number, or a string, or something else. You can however have an array of strings, and an array of numbers, and consider the information to be "sliced" together, like reading a table by columns instead of rows )).
Last edited on Dec 13, 2011 at 2:46am
Dec 13, 2011 at 2:55am
We just started learning about arrays...and I kind of understand that, and that means that I have at least started this correctly...but it's still confusing me about how to call them and use them...i don't know. Thanks though. :D
Dec 13, 2011 at 3:14am
Ok, I had to gauge what level to pitch this at (^_^)... (feel free to skip the bits you recognise).

A variable normally has a single value, of a single type. eg

int indexOfSmallest = 0;
defines a variable called 'indexOfSmallest', of type integer, with a value zero.

An array normally contains multiple values, of a single type. eg

int sales[NUM_TYPES];
defines a variable called 'sales', being an array of integers, containing NUM_TYPES entries, numbered from 0 to NUM_TYPES-1. This is the first big thing to remember about C++ (and C) arrays - arrays always start with index 0 (_not_ 1). Consequently, when you declare an array of 5 entries, you access them via sales[0], sales[1], sales[2], sales[3] and sales[4]. It's a common mistake (called the 'off by one' error) to try and access sales[5] (which will return garbage). Don't do that! (^_^)h.

This also has an effect on loops, when you want to walk across all elements of an array. If you have a constant for the number of elements, your loops will ALWAYS be of the form:

for (int type = 0; type < NUM_TYPES; type++)
Note that type starts at 0, and the test condition is less-than (not less-than-or-equal!). That loop can be used to safely access elements of an array declared like sales[] above.



To use the element of an array, you use the square-brackets notation (as I've been doing above). Inside the brackets is the index of the element you want to access. So,

sales[0] = sales[0] + 1;
will access the first element of the sales array, add one to it, and store it back in that element again. Of course, that's pretty boring, but it's the first step. Now you don't always have to access a constant element (like I did just above), you can do things like:

sales[type] = 0;
which (assuming it appears within a loop like the for-loop I quoted earlier) sets each element of the sales array to 0.



One other thing you should know - you can initialise arrays using an "initialisation list". This is a list of values contained within braces, one for each element of the array. So, you can do something like:

int sales[NUM_TYPES] = { 1, 1, 1, 1, 1 };
which would set each element of the sales array to the value 1 (perhaps to indicate that you've already sold one item to your test client or something ;-).



(( btw, I'm not going to write your code for you - I'm hopefully just going to give you sufficient understanding to handle the challenge yourself. ))
Dec 13, 2011 at 3:23am
Thanks a lot! I don't want you to write my code for me, lol. I just am having trouble with this chapter. I appreciate all of the explanations you gave, I think I may be able to handle it now...I hope! :P
Dec 13, 2011 at 3:39am
Um...I've got a question about using strings with them. Why would this not work? I realize that the const is an int...but I'm not sure how to incorporate the strings in there without defining them first...should I have to define them?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
   string mild, medium, sweet, hot, zesty;
   const int NUM_TYPES = 5;   
   int sales[NUM_TYPES];
   string name[NUM_TYPES] = {mild, medium, sweet, hot, zesty};

 
   // Input the number of jars sold

	for (int type = 0; type < NUM_TYPES; type++)
	{
		cout << "Jar sold last month of " << name[type] << ": ";
      	cin  >> sales[type];
      
		while (sales[type] < 0)
		{	cout << "Jars sold must be 0 or more.  Please re-enter: ";
		   cin  >> sales[type];
		}
	}
Dec 13, 2011 at 4:02am
You don't need to 'define' strings like that. Strings are contained within double-quotes (like "Jar sold last month of " is a string). Consequently, you don't need line 3, and you do need to put each item within double quotes, like "mild", "medium", etc.
Dec 13, 2011 at 4:06am
Ahhh! Thank you...I knew that...Idk why I didn't think of it. I think I'm tired. Thanks very much.
Dec 13, 2011 at 4:11am
No worries -- get some sleep, better not try and code while tired (at least, not until you're much more experienced ;-). I have to take a pause from reading this thread for about an hour too. (^_^)n
Dec 13, 2011 at 4:46am
Lol...I figured it out! :D Thank you so much for your help. I'm trying my best to learn. :P
Here's my final code if you want to see! :D

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Chapter 8 - Chips and Salsa
// This program produces a sales report for a salsa maker who
// markets 5 types of salsa. It includes total sales for all
// products and identifies the highest and lowest selling product.
// Kara Pardue 
// 12/12/2011
// The program should use 2 parallel arrays, one for string names, and one for integer sales. 
// It should call on three functions, one to get the total sales, one for highest selling salsa, and one for lowest selling.
// These three functions should use the arrays in order to return their data.
// The program should then print all of the data, including salsa type, sales of each, total sales, and highest and lowest of each.

#include<iostream>
#include <iomanip>
#include <string>
using namespace std;

// Function prototypes
int getTotal(int [], int);

int posOfSmallest(int [], int);

int posOfLargest(int [], int);

int main()
{
   const int NUM_TYPES = 5;   
   int sales[NUM_TYPES];
   string name[NUM_TYPES] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
  

   // Create the arrays for the names and sales amounts
   
   int totalJarsSold,
       hiSalesProduct,
       loSalesProduct;
     
   // Input the number of jars sold

	for (int type = 0; type < NUM_TYPES; type++)
	{
		cout << "Jars sold last month of " << name[type] << ": ";
      	cin  >> sales[type];
      
		while (sales[type] < 0)
		{	cout << "Jars sold must be 0 or more.  Please re-enter: ";
		   cin  >> sales[type];
		}
	}

   
   // Call functions to get total sales and high and low selling products
   totalJarsSold  = getTotal(sales, NUM_TYPES);
   
   loSalesProduct = posOfSmallest(sales, NUM_TYPES);

   hiSalesProduct = posOfLargest(sales, NUM_TYPES);
   
   // Produce the sales report
   cout << endl << endl;
   cout << "     Salsa Sales Report \n\n";
   cout << "Name              Jars Sold \n";
   cout << "____________________________\n";
 
   
	   cout << name[0] << "                  " << sales[0] << "\n";
       cout << name[1] << "                " << sales[1] << "\n";
	   cout << name[2] << "                 " <<sales[2] << "\n";
	   cout << name[3] << "                   " << sales[3] << "\n";
	   cout << name[4] << "                 " << sales[4] << "\n";  

      // insert the code that prints the salsa names and number jars sold
   
   cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
   cout << "High Seller: "  << name[hiSalesProduct] << endl;
   cout << "Low Seller : " << name[loSalesProduct] << endl;    
    
   system("pause");
   return 0;
}

/************************************************************
 *                       	getTotal                         *
 * Calculates and returns the total of the values stored in *
 * the array passed to the function.                        *
 ************************************************************/
int getTotal (int array[], int numElts)
{
	int total = 0;

	for (int type = 0; type < numElts; type++)
		total += array[type];
	return total;
}

/************************************************************
 *                    posOfLargest                          *
 * Finds and returns the subscript of the array position    *
 * holding the largest value in the array passed to the     *
 * function.                                                *
 ************************************************************/
int posOfLargest(int array[], int numElts)
{
	int indexOfLargest = 0;
	
	for (int pos = 1; pos < numElts; pos++)
	{
		if (array[pos] > array[indexOfLargest])
			indexOfLargest = pos;
	}
	return indexOfLargest;
}

/************************************************************
 *                    posOfSmallest                         *
 * Finds and returns the subscript of the array position    *
 * holding the smallest value in the array passed to the    *
 * function.                                                *
 ************************************************************/
int posOfSmallest(int array[], int numElts)
{
	int indexOfSmallest = 0;
	
	for (int pos = 1; pos < numElts; pos++)
	{
		if (array[pos] < array[indexOfSmallest])
			indexOfSmallest = pos;
	}		
	return indexOfSmallest;
}

/* 
Jars sold last month of Mild: 34
Jars sold last month of Medium: 89
Jars sold last month of Sweet: 63
Jars sold last month of Hot: 109
Jars sold last month of Zesty: 47


     Salsa Sales Report

Name              Jars Sold
____________________________
Mild                  34
Medium                89
Sweet                 63
Hot                   109
Zesty                 47

Total Sales:            342
High Seller: Hot
Low Seller : Mild
*/
Dec 13, 2011 at 5:06am
Looks good! (^_^)d.

One minor observation - you could rewrite lines 65-69 slightly more compactly, you know ;-).

Hope you enjoy the rest of your course (^_^)/
Dec 13, 2011 at 2:41pm
i hv information about c so i need simple programmes of c++ from where i can got these
Topic archived. No new replies allowed.