I'm very confused when it comes to arrays and I've got a mini-project on using them but i'm stuck at a certain part in my program and I don't know what to do next, can anyone help?
the question is:
"Write a C++ program that reads 5 integers from the screen (provided by the user) and determines the largest integer. You MUST use an array to store the 5 integers.
The following shows a sample output of the program.
Enter 5 integers: 15 36 -8 92 56
The largest integer is 92 "
what i've got so far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int userIntegers[5];
cout<<"Enter 5 integers: ";
cin>>userIntegers[0];
//system("pause");
return 0;
}
Here is the thing you have to do. You need to use a FOR or WHILE loop to get the certain number of user input and store them in array.
int userIntegers[5];
int largest = 0;
cout<<"Enter 5 integers: ";
for (int i=0; i<5; i++) //Use for loop upto how many numbers you need to get as input.
{
cin>>userIntegers[i];//get the input from user and store it in array at the index
/*If the input is the larger than prev largest or For special case to handle if all the values entered is less than zero.*/
if(largest < userIntegers[i] || largest == 0)
{
largest = userIntegers[i];//Assign the largest number to the variable.
}
}
cout<<"Largest Integer is: "<<largest;
or you can do more easily (using isstringstrem and INT_MIN):
int maxnumber = INT_MIN; // for being sure to have at lest one number above
int number;
string s;
cout<<"Enter 5 integers: ";
cin >> s;
std::istringstream steam( s );
while(steam >> number) {
if (number > maxnumber) {
maxnumber = number;
}
}
EDIT:
If you need an array #Sridhar seem have your answer (but think about using INT_MIN http://www.cplusplus.com/reference/climits/)
Related
In c++, I have to write a code to get the user's desired numbers in each line with "while" command and when the user enter number -1 at the end, I have to display the largest number, the largest number other than the previous number, and the number of row with the largest number entered. For example user's numbers is:
4
7
11
5
-1
and result is:
11(the biggest number)
7(the biggest number after 11)
3(the row that the user has entered the largest number)
This is my own code:
#include <iostream>
using namespace std;
int main()
{
int price;
int max=0;
cin>>price;
while(price!=-1)
{
while(price>max)
{
max=price;
}
cin>>price;
}
cout<<max;
return 0;
}
I can find the largest number, but not the other two variables. Please reply ASAP.
I don't want to spoon feed you the code. I will help you with the logic instead.
Take input and store it in an array.
Then use a sorting algorithm (there are tons of them, another opportunity for you to learn) to sort the array (or the vector)
Print the array
View the code only when you learn how to do it on your own:
std::vector<int> vec;
int input = 0;
while (input != -1) {
std::cin >> input;
vec.push_back(input);
}
std::sort(vec.begin(), vec.end());
for (auto& element : vec) {
std::cout << element << " ";
}
My task is to find the minimum number between n input values that the user should enter in an infinite loop until a certain number or character is entered to stop the loop.
The problem that I am facing is, I can't get the condition that tests the input to see which of the entered numbers is the smallest to work. Also, a second problem is, I want to end the loop with a char not an int, but I don't know if that is even possible.
I searched online but I can't find any answers.
Side note: I am new to C++. I am using Borland C++ v5.02.
#include <iostream>
#include <conio.h>
int I, min =0;
cout<<"Enter a number :";
do{
cin >> I;
if (I < min){
if (I > 0){
min = I;
}
}
}while (I > -1);
cout << min;
I solved your problem by using a try-catch block and stoi().
stoi() is used to convert a string into a number. If the number input is not convertible (meaning that a char is entered and the loop should break), const std::invalid_argument & e is catch and automatically break the loop.
#include <iostream>
using namespace std;
int main()
{
int Min = INT_MAX; string I; int x;
do
{
cout << "Enter a number or a char : ";
cin >> I;
try
{
x = stoi(I);
if (x < Min)
{
if (x > 0) {Min = x;}
}
}
catch(const std::invalid_argument & e) {break;}
}
while(x > 0);
cout << "Minimum positive number entered : " << Min;
}
Output:
Enter a number or a char : 10
Enter a number or a char : 8
Enter a number or a char : 5
Enter a number or a char : 7
Enter a number or a char : a
Minimum positive number entered : 5
As your code is a bit unclear, I changed both constraint to I>0, and you can easily modified this bit.
For the prolem with INT_MAX, maybe #include <climits> or #include <limits.h> will help, as specified here. If the problem persist, the workaround is to set Min to something high, for example 10^9.
*Note: Ran on Code::Blocks 20.03, Windows 10 64-bit.
the problem with the code was with the header I couldn't find a one that was working with my compiler Borland v5.02 c++ but thanks to #JerryJeremiah he leads me to it.
also, I redeclared the min =INT_MAX; because I am using this code in a loop.
the code is working with me now.
#include <iostream>
#include <conio.h>
#include <limits.h>
int main()
{
int I, min =INT_MAX;
cout<<"Enter number of input :";
do{
cin>>I;
if (I<min ){
if(I>0){
min =I;
}
}
}while(I>-1);
cout<<min;
min =INT_MAX;
getch();
}
Picture of what the program is supposed to do:
#include <iostream>
using namespace std;
int main(){
int nums[10],n,i=0;
int s[10],popS[10];
int sum=0;
int numSum;
How can I enter an unknown amount of values for array nums with a termination value of -1?
cout<<"Elements being placed in Stack,s"<<endl;
for(int i=0;i<10;i++){
push(s,nums[i]);
}
cout<<"Popping the stack..."<<endl;
for(int i=0;i<10;i++){
popS[i]=pop(s);
}
for(int i=0;i<10;i+=2){
numSum=popS[i];
sum+=numSum;
}
cout<<"The sum for every other element pop is "<<sum<<endl;
return 0;
}
I am trying to enter values into array nums but the number of values isn't supposed to be known and terminated by -1.
This will accept user input until 10 ints have been received, or the user enters -1. The values will be placed in an array. User input is not checked to see that it is a valid int. Afterwards, n will hold the number of valid integers entered.
int nums[10];
std::size_t n = 0;
for (; n < 10; n++)
{
int input;
std::cin >> input;
if (input == -1)
{
break;
}
nums[n] = input;
}
This will accept the first 10 ints from user input until a -1 is entered. Any numerical input beyond the 10th will be ignored. User input is checked to see that it is a valid int in range. Any invalid input will exit the input loop. Afterwards, n will hold the number of valid integers entered, up to 10. If an invalid conversion was attempted (such as entering non-numeric input or a number out of range), handle the error
int input;
int nums[10];
std::size_t n = 0;
while (std::cin >> input)
{
if (input == -1)
break;
if (n < 10)
{
nums[n++] = input;
}
}
if (std::cin.fail())
{
std::cin.clear(); //clear the fail and bad bit
std::cin.ignore(INT_MAX); // ignore any of the pending input that came after the offending conversion pattern
std::cout << "Error reading input.\n";
...do some meaningful error handling. Exit the program likely.
}
I want to input any number into array b[] by number of numCase times.
#include <iostream>
using namespace std;
//entry point
int main()
{
//Declarations
int b[20]; // array size 20 ( limit of inputs)
int c = 0;
int numCase;
int input;
cout << "ENTER NUMBER OF CASES (MAXIMUM NUMBER OF 20): \n";
cin >> numCase;
//checks that numCase is less than or equal to (20) and does not exceed
if (numCase < 21)
{
// gets input number based on the numCase
do
{
cout << "ENTER A NUMBER (MAXIMUM OF 5 DIGITS): \n";
cin >> input;
cout << "\n";
b[c] = input;
c++;
} while (c != numCase);
cout << b[c] ; // this is my problem it OUTPUTS RANDOM VALUE,
//but i can see on my watch list that b has the values of my input.
}
}
You're filling entries 0 toN of b, and then printing entry N+1, which you haven't filled in.
The variable c should be initialised back to zero.
} while (c != numCase);
c = 0;
cout << b[c] ; // in this statement c already reached to numCase where you have not assigned any value
I think this is what you might be looking for:
for (int i=0; i<numCase; i++)
{
if(b[i] >= x) //x is a variable that u can set as a limit. eg. 700
{
cout<<"\n"<<b[i];
}
}
Hope it helps
If you want to display all the numbers stored in array b[]
then you may write your code as
for(int i=0;i<=20;i++)
{ if(b[i]<101) //This will exclude all the values which are greater than 101
{cout<<"\n"<<b[i];}}
Hello I am having trouble with a c++ program. Basically its a loop that iterates the amount of times the user wants it to. Now when it reaches a number divisible by 4 it keeps track of that number and finally then outputs how many times the number entered was divisible by 4.
#include<iostream>
using namespace std;
int num;
int count;
int test = 0;
int main()
{
cin>> num;
for (int count = 0; count < num; count++)
if (count % 4 == 0)
(test++);
else
cout<<"";
return 0;
}
Well - if you use return in main, your program will just exit, because that's what return does - ends the function and returns some value. If you want to actually print the value of test, do it before you return:
cout << test;
getch(); // use this so the console won't close automatically
return 0;
Also, the whole program could be written much better:
int main()
{
cin>> num;
cout << num/4;
getch(); // use this so the console won't close automatically
return 0;
}
Do you need to use a loop? If you just need "How many times is a given number divisible by 4" and are not required to loop
#include<iostream>
using namespace std;
int main()
{
int num;
cin>> num;
cout<< num<<" is divisible by 4 "<< (num>>2) <<" time"<<(num>>2>1?"s":"") <<endl;
return 0;
}
num>>2 is bit shifting to teh right twice, which is the same as doing an integer divide by 4. It could be replaced by num/4 if you wanted. Integer division always truncates, so for all positive numbers, it's like rounding down: the same behavior your loop gives you.