Question
Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the largest integer among them and print it on the screen. Typical output screen should be as following:
Note: the code must contain function to find the largest number.


Enter four integers 1 2 4 0 Largest integer is 4 03 (35 points)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<string>

#include<sstream>

using namespace std;

int largestNumber(int b[],int size){

int c;

c=b[0];

for(int i=0;i<size;i++){

if(b[i]>c)

c=b[i];

}

return c;

}

int main() {

  

cout<<"Enter four integers seperated by comma ',': ";

string my_str;

int a[4],j=0;

cin>>my_str;

const char d=',';

std::stringstream ss(my_str);

std::string s;

while (std::getline(ss, s, d)) {

a[j]=stoi(s);

j++;

}

cout<<"Largest Integer is: "<<largestNumber(a,4);

}

Output:

5 input CO Enter four integers seperated by mma : 4,5,3,2 Largest Integer is: 5

If output required as per given in question with input entry with spaces, then use below code.

#include<iostream>

#include<string>

#include<sstream>

using namespace std;

int largestNumber(int b[],int size){

int c;

c=b[0];

for(int i=0;i<size;i++){

if(b[i]>c)

c=b[i];

}

return c;

}

int main() {

  

cout<<"Enter four integers ";

string my_str;

int a[4],j=0;

getline(cin,my_str);

const char d=' ';

std::stringstream ss(my_str);

std::string s;

while (std::getline(ss, s, d)) {

a[j]=stoi(s);

j++;

}

cout<<"Largest Integer is "<<largestNumber(a,4);

}

Output:

input Enter four integers 1 4 3 2 Largest Integer is 4

Add a comment
Know the answer?
Add Answer to:
Write a complete C++ program to ask the user to enter 4 integers. The program must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a complete C++ program to ask the user to enter 4 integers. The program must...

    Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the smallest integer among them and print it on the screen. Typical output screen should be as following: Write a complete C++ program to ask the user to enter 4 integers. The program must use a functionto find the smallest integer among them and print it on the screen. Typical output screen should be as following: Note: the code...

  • Write a complete C++ program to ask the user to enter 4 integers. The program must...

    Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the LARGEST integer among them and print it on the screen. Typical output screen should be as following: Note: the code must contain function to find the LARGEST number. Enter four integers 1 2 4 0 Largest integer is 4

  • Write a complete C++ program to ask the user to inter two integers and the program...

    Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Enter two integers 18 27 The GCD is = 9

  • Write a complete C++ program to ask the user to inter two integers and the program...

    Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Enter two integers 18 27 The GCD is = 9

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • write in python Create a program that will ask the user for a list of integers,...

    write in python Create a program that will ask the user for a list of integers, all on one line separated by a slash. construct a list named "adj_numbers" which contains the users input numbers after subtracting 2 from each number. then print the resulting list. the input statement and print statement are given to you in the starting code. a sample run of the program is as follows: Enter a list of integers, separated by slashes: 7/3/6/8 resulting list:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT