Find largest and smallest in array without sorting

Hi guys,
I have wrote a program that allows users to enter item names and number of each item sold. One of the outputs i want to give is the most and least sold item. The item name, item price and number sold are arrays with another array for item number, they can enter up to 5 items and prices. I want to be able to find the smallest and largest number in the number sold array so i can print the most sold and least sold item. Only a beginner so if im way off base please tell me.

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

#include "stdafx.h"
#include <string>
#include <conio.h>
#include <iostream>
#include <array>

using namespace std;

string sItems[5] = {};
string sChoice = "";
string sMostSoldItem = "";
string sLeastSoldItem = "";
string sReviewProduct = "";
int iItemPrice[5] = {};
int iNumSold[5] = {};
int iTotalItemsSold = 0;
int iMostSoldvLeast = 0;
int iTax = 0;
int iProfit = 0;
int iNumOfItems = 0;
int iMenuChoice1 = 0;
int iItemNum = 0;
int iCount = 0;
bool bRepeat = true;

int main()
{
	while (bRepeat = true) { //loops back to menu 

		cout << "--------- MAIN MENU ---------\n\n Please pick from an option below: \n 1: Enter new items \n 2: Change item prices \n 3: Input sold items \n 4: All items and prices \n 4: Receipt for previous items sold\n";
		cin >> iMenuChoice1;

  switch (iMenuChoice1)
		{
		case 1: { // Enter New items
			do {

				cout << "--------- ENTER NEW ITEMS ---------\n\nPlease enter the item Name: ";
				cin >> sItems[iCount];
				cout << "\nPlease enter the price of: " << sItems[iCount] << "\n";
				cin >> iItemPrice[iCount];
				cout << "\nWould you like to enter another item? Y/N \n";
				cin >> sChoice;

				if (sChoice == "Y" || sChoice == "y")
				{
					++iCount;
					++iNumOfItems;
				}

			} while ((sChoice == "Y" || sChoice == "y") && iNumOfItems < 5);
			break;
		}

case 3: // Input Sold items
		{
			do {
				cout << "--------- INPUT SOLD ITEMS ---------\n\nPlease select an item: \n1: " << sItems[0] << "\n2: " << sItems[1] << "\n3: " << sItems[2] << "\n4: " << sItems[3] << "\n5: " << sItems[4] << "\n";
				cin >> iItemNum;
				cout << "\nPlease enter the ammount sold: " << sItems[iItemNum - 1] << "\nQuantity: ";
				cin >> iNumSold[iItemNum - 1];
				cout << "\nyou have sold " << iNumSold[iItemNum - 1] << " of " << sItems[iItemNum - 1];


				cout << "\nWould you like to input more sold items? Y/N \n";
				cin >> sChoice;
			}while (sChoice == "Y" || sChoice == "y");

			break;
		}
it looks like you just need to find the index of the max and min values in inumsold array and tie that back together into a print statement. so if inumsold[3] is the smallest, then print the least sold was item[3] with a count of numsold[3] ...

you will need a loop to find that 3 (for example) was the least and 1 (example) was the most.

It mostly looks like a good start but you may want to open it up so that they can input amount sold for the same item multiple times. It over-writes currently.
Last edited on
This is pretty good so far. For a new feature of a program, consider isolating it into its own function, completely commenting everything out that you have so far until the function works. You can do this easily with #if 0 . For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

// Returns index of most sold item in a 5-item array
int MostSold(int sold_items[5])
{
    // TODO: write some logic to do this!
    return 0;
}

int main() 
{
#if 0
    // Everything you have in here so far
#endif
    
    string item_names[] = {"Markers", "Paper", "Notebooks", "Candy", "Milk"};
    int num_sold[] = { 1, 5, 13, 6, 7};
    
    int index = MostSold(num_sold);
    cout << "Most sold: "<< item_names[index] << "("<<
            num_sold[index] << ")\n";
}


Current output:
Most sold: Markers(1)


Stylistic Notes:
-- You may find it easier on the eyes to set your editor to "Convert tabs to spaces", and
-- Set the number of spaces to 4. People have different preferences, but this is a common value I see mentioned, especially for C-family languages. It's easier to copy over to emails, forum posts, etc.
-- For legacy reasons, there are a lot of people who like to keep their line length to about 80 columns. These days, something like 100 columns probably doesn't hurt, /shrug, and yeah, a lot of modern editors have nice word-wrapping support, but sometimes you'll run into places where long lines don't look so good. To help keep column length lower, note that cout parts can be chained together like so (and you may prefer for visual reasons to write endl instead of \n to mark the end of a line):
1
2
3
4
5
6
7
cout << "--------- MAIN MENU ---------" << endl << endl <<
        "Please pick from an option below:" << endl <<
        "1: Enter new items" << endl <<
        "2: Change item prices" << endl <<
        "3: Input sold items" << endl << 
        "4: All items and prices" << endl <<
        "5: Receipt for previous items sold" << endl;
Last edited on
Topic archived. No new replies allowed.