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...

  • Write a program that finds the largest and smallest of four integers entered by user. Enter...

    Write a program that finds the largest and smallest of four integers entered by user. Enter four integers: 21 43 10 35 Largest: 43 Smallest: 10 (Must be done in Code::Blocks C++)

  • 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

  • Four Integer Stats Write an ARM Assembly Language (I will not accept Intel Assembly code) program...

    Four Integer Stats Write an ARM Assembly Language (I will not accept Intel Assembly code) program to prompt the user to enter four integers. Have your program output to the screen the four integers that were entered at the keyboard, along with the following: sum of the four integers, smallest value, largest value, and the average of the four values. You must utilize the scanf function for reading in the user input and the printf function for outputting the results...

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