How to find the four lowest numbers



thx
Last edited on
You have already made a bug.:) The four lowest numbers will be 10, 20, 30, and 35.:)
Thx, any suggestion?
I am suggesting to declare an array of four elements to keep the minimums.:)
Hi there, i'm beginner as well but i hope this 2 examples will help you with how to write/read to external txt file.
this is how you can open/create txt file called dataFile and how you can make it write data with separation among seperate values in txt. To make program quit press Ctrl+z;

you have to include #include <fstream> to be able to work with files

1
2
3
4
5
6
7
8
9
10
11
12
13
      {
               int idNumber;
	string name;
	int power;

               ofstream myFile("dataFile.txt");

	cout << "enters player ID, Name, and Power" << endl;
	cout << "press Ctrl+z to quit\n" << endl;

	while(cin >> idNumber >> name >> power){
		myFile << idNumber << ' ' << name << ' ' << power << ' ' << endl;
	}


this is how you can read data from a file and use it to print it on screen.

1
2
3
4
5
6
7
8
9
10
11
{
ifstream myFile("dataFile.txt");

	int id;
	string name;
	int power;

	while(myFile >> id >> name >> power){
		cout << id << ' ' << name << ' ' << power << endl;

	}


hope it helps
Last edited on
hey i was intrigued with your question about makeing a code that would be able to find the lowest values in a array of any size, so i gived it a try.

Must say this was the moste fun thing i did in these 2 months of learning to program, unlike so far this time i really needed to see how the hek to tell a computer to find the lowest values in a array, and i'd like to think i found adequate sollution.

Now this might not be the moste efficient way to code it but it works, infact i belive that this same code could be expanded to work with arrays and vectors as class and use it as function that can deliver any amount of Lowest, Equals or Highest values from it.

btw, srry if i didn't made good comments, it took me 30min to find sollution by empiricly testing few ways to get the resault but it took me an hour trying to make good comments to make it understandable. I'm missing expiriance hehe

also you can demand any amount of lowest number you need as long its not higher than array size.

anyways, check it out, hope thats what you were looking for :)

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
//============================================================================
// Name        : findLowest.cpp
// Author      : Ironman
// Version     :
// Copyright   : Your copyright notice
// Description : lowestValueFinder
//============================================================================

#include <iostream>
using namespace std;

int main()
{
	int myArray[20] = {10, 120, 70, 99, 4, 15, 83, 9, 13, 64, 28, 57, 69, 43, 2, 17, 72, 19, 94, 101}; 	//array
	int arraySize = 20; 	//array size
	int howManySmallValues = 4; //how many smaller numbers are you looking for
	int minLimit = (arraySize-howManySmallValues);  //minimum number of times the element has to be smaller than other array elements
													//to be considered as one of smallest

	//sets current array element
	for(int y = 0; y < arraySize; y++)
	{
		int isSmaller = 0; // remembers the number of times when the current element value was smaller than other element value

		//compares current array element with every element in the array
		for(int x = 0 ; x < arraySize; x++)
		{
			if(myArray[y]<myArray[x]){ //if the current element is smaller than other element value add 1 to isSmaller variable
				isSmaller++;
			}
		}
		//if element was smaller than other elements same or more times than the minLimit print that element as one among smallest
		if (isSmaller >= minLimit)
		{
			cout << myArray[y] << endl;
		}
	}
}


10
4
9
2


it takes only continuation of if statment to get highest and equal value as well if needed
1
2
3
4
5
6
7
if(myArray[y]<myArray[x]){ //if the current element is smaller than other element value add 1 to isSmaller variable
				isSmaller++;
			}else if(myArray[y]>myArray[x]){
				//this can be used to find highest values
			}else{
				//this means it found equal value( it can be the value it self thats beeing tested or another element with same value
			}


Last edited on
OP, for reference:

Dear all,

I am a C++ beginner and I encountered a problem that is "Read in a comma separated list of decimals from a file calculate the average of the 4 lowest numbers and print that value formatted as a dollar amount to a separate file."

For example: 30, 60, 35, 40, 50, 100, 10, 20
The average is (10+ 20+ 30 + 40)/4= 25

My thinking is to use quick sort to find the four lowest numbers. Is that make sense? I am also confused with how to format a value to a dollar amount? Any help will be appreciated.
thx for OP Moschops, i knew there was something about 4 lowest numbers. Guess all he would need once the lowest was determen is to pass the values to vector, make sum of vector values/vector size. Tho not sure what value formatted as dollar amount means, maybe just "$" sign after the value?
Last edited on
Topic archived. No new replies allowed.