here it goes:
Make a program that will input 5 integers and the
output is:
-the total is:
-the largest integer is:
-the smallest integer is:
-the total of the integers if there is negative:
*our instructor told us that we should use array statement and if else statement...
please...please...please...can anyone help me what is the structure of ds program?
i hope u know wat i mean...thank u so much for the help!
Turbo c++...Programming...please help! please...please...i'll give 10 pts!?
Not sure if you want functions or anything fancy so its all going into a main. The program will look something like this... I didnt attempt a compile so dont expect it to be perfect.
#include %26lt;iostream%26gt;
using namespace std;
int const size = 5;
int main()
{
int num[size];
for (int i = 0; i %26lt; size; i++)
{
cout %26lt;%26lt; "Enter integer #" %26lt;%26lt; i %26lt;%26lt; ": ";
cin %26gt;%26gt; num[i];
}
int total;
total = num[0] + num[1] + num[2] + num[3] + num[4];
cout %26lt;%26lt; "Sum of integers is: " %26lt;%26lt; total %26lt;%26lt; endl;
int largest, smallest;
largest = num[0];
smallest = num[0];
for (int h = 0; i %26lt; size; i++)
{
if (num[h] %26lt; smallest)
smallest = num[h];
if (num[h] %26gt; largest)
largest = num[h];
}
cout %26lt;%26lt; "The largest integer is: " %26lt;%26lt; largest %26lt;%26lt; endl;
cout %26lt;%26lt; "The smallest integer is: " %26lt;%26lt; smallest %26lt;%26lt; endl;
return 0;
}
Reply:I believe that Josh's answer will work, but I would check for the smallest and largest as the integers are coming in. I'd also work on the total and have two ideas because i am not sure if you only want the total if one of the integers is negative or not so here is my revision:
#include %26lt;iostream%26gt;
using namespace std;
#define TRUE=1;
#define FALSE=0;
int const size = 5;
int main()
{
int num[size]; //the array for incoming numbers but is really not needed, you can write this without an array
int i; // for counting the incoming numbers
int total; //to find the sum of the incoming numbers
int largest; // the largest integer
int smallest; //the smallest integer
bool negative; // this will be set to true if there is a negative number input - I use 0 for false and 1 for true
i=0; //this will get the first number
cout %26lt;%26lt; "Enter integer #" %26lt;%26lt; i %26lt;%26lt; ": ";
cin %26gt;%26gt; num[i];
largest=num[0];
smallest=num[0];
total=num[0];
if (num[0]%26lt;0) negative=TRUE else negative=FALSE;
for (i = 1; i %26lt; size; i++)
{
cout %26lt;%26lt; "Enter integer #" %26lt;%26lt; i %26lt;%26lt; ": ";
cin %26gt;%26gt; num[i];
if (num[i] %26lt; smallest) smallest = num[i];
if (num[i] %26gt; largest) largest = num[i];
total=total+num[i];
}
cout %26lt;%26lt; "The largest integer is: " %26lt;%26lt; largest %26lt;%26lt; endl;
cout %26lt;%26lt; "The smallest integer is: " %26lt;%26lt; smallest %26lt;%26lt; endl;
// if you want to print out the total no matter what, leave out the if statement and no need for the boolean variable
if (negative=TRUE) cout %26lt;%26lt; "The total of the integers is: " %26lt;%26lt; total %26lt;%26lt; endl;
return 0;
}
Reply:I really don't know.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment