Haha, i'm washing my hair with hands all the time from last few days. If possible could any body give me some hints where do i start with these assignment.
This all i need,
1. input few number (no limit, as many as i can)
2. calculate them
seem to be much harder than excel..................................
you can create an integer variable that initialize to zero, when input is entered, it increase by 1
2. Sum of all positive numbers
you can use if statement, if(num > 0) then you sum them
3. Average of first and last numbers
i prefer to use array here.. create an empty array, when input data is entered, stored in the first position in the array, example array[count] = num, count is zero, mean your first data in array, num is your input...
4. 70% of smallest positive number
sorry, i don't understand what it really wants
5. Largest number entered
you can create an integer variable to store max value, but have to initialize to zero first
use if statement here, if(num > max) max = num, when input data greater than max, the max = num
6. The number of times the largest number is entered
Honestly these in real life and logical is very easy to understand, i can do it with excel in no time.
But in C++ is kind of different story, i'm not familiar with the code anyway
Could you help me to create first out put which is counting numbers?
Grax, the most important part which i still do not understand is how C++ store value and how can i put more than value and after than cout or sum or what ever with those value. It is unclear that way.............
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
float input = 0,
number = 0,
sumpositive = 0,
average = 0,
smallestpositive = 0,
percentage = 0,
largest = 0;
}
I don't quite see how C++ even compares to excel. One is a programming language, the other is an office application. o_O
i still do not understand is how C++ store value
Well seems you are good with excel, so I'll try to explain it in an excel way.
In excel we have cells that can hold values, in C++ was have variables.
Whenever you create a program in C++ imagine you don't have any cells at the start. It would be like opening an excel file and not seeing any cells. So if you want to have a "cell" to store a value, you need to declare a variable like:
float input; // creates the "cell"
or
float input = 0; // creates the "cell" and automatically inserts a value of 0
that would create a "cell" that can store numbers with decimal points, and what's different in C++ is you can give that cell a nickname. So you can use the nickname instead of using A, 1 or A,2 etc. Anyways, if you want to set the value manually, you use the assignment operator like:
input = some value;
or you can make the user of your program enter the values he want like so:
cin >> input;
Anyway, that's how C++ store values if that makes any sense.
ryantoss, you're welcome, actually i'm also a newbie, we can all learn c++ together , sharing ideal here ^.^
1 2 3 4 5 6 7 8 9
int maxCount = 0;
for (int i = 0; i < count ; i++) //count will be the size of the array
{
if (max == array[i]) //check max number in array
{
cout << max << " " << array[i] << endl;
maxCount++;
}
}
i think this code will give you an ideal in part 6 (not actual answer), and about part 7, actually which two numbers it wants?
// Created by Tuan Minh Pham s0222607 CQU University course COIT0222607 assignment 1
// The program was created in order to caculate series of any numbers which you enter and displays these output
// 1. The number of values entered
// 2. Sum of all positive numbers
// 3. Average of first and last numbers
// 4. 70% of smallest positive number
// 5. Largest number entered
// 6. The number of times the largest number is entered
// 7. The two numbers in descending order
#include <iostream> //Start of program
usingnamespace std;
int main ()
{
constint terminate = -7777; //Where all my higest number in s0222607 is 7
float input = 0, //The start of variables, all value start from 0 and use float data type
count = 0,
sumpositive = 0,
firstnumber = 0,
lastnumber = 0,
average = 0,
smallestnumber = 0,
largestnumber = 0,
largestcount = 0,
secondlast = 0;
cout << "This program was created for calculating series of any number which you enter and display these ouput:" << endl;
cout << "1. The number of values entered" << endl;
cout << "2. Sum of all positive numbers" << endl;
cout << "3. Average of first and last numbers" << endl;
cout << "4. 70% of smallest positive number" << endl;
cout << "5. Largest number entered" << endl;
cout << "6. The number of times the largest number is entered" << endl;
cout << "7. The two numbers in descending order" << endl << endl << endl;
cout << "Start of program" << endl << endl << endl;
cout << "Program input: " << endl << endl;
do
{
cout << "Enter a number or -7777 to end: ";
cin >> input;
cout << endl;
if ( input != terminate)
{
count = count + 1;
lastnumber = input;
if ( input >0)
{
sumpositive += input;
if ((smallestnumber > input) || (count == 1))
{
smallestnumber = input;
}
}
if (count == 1)
{
firstnumber = input;
}
if (input > largestnumber)
{
largestnumber = input;
}
if (input = largestnumber)
{
largestcount = largestcount + 1;
}
}
}
while (input != terminate);
average = (firstnumber + lastnumber) /2;
cout << "Program output: " << endl << endl;
//Start of display ouput
if (count == 0) //There is no value
{
cout << "No values were entered, no further calculation is processed";
cout << endl;
cout << endl;
}
else
{
cout << endl;
cout << endl;
if (count == 1) //There is 1 value
{
cout << "Only 1 value was entered, no further calculation is processed";
cout << endl;
cout << endl;
}
else
{
cout << endl;
cout << endl;
}
if (count > 1 ) //The start of display calculation, actually calculation was processed but not displayed
{
cout << "1. "; cout << count; cout << " numbers were entered"; cout << endl;
cout << "2. "; cout << "The sum of all positive values entered is " << sumpositive ; cout << endl;
cout << "3. "; cout << "The average of the first and last numbers is " << average; cout << endl;
cout << "4. "; cout << "70% of the smallest positive value number is " << (smallestnumber*70)/100; cout << endl;
cout << "5. "; cout << "The largest value entered was " << largestnumber; cout << endl;
cout << "6. "; cout << "The largest value entered occurred " << largestcount; cout << " times "; cout << endl;
}
}
} //End of program
I know that, but the OP was asking why the code he posted wasn't working, that's why I said he needs to include stdlib if he wants to use system("pause"). I never suggested he use it though, and I did tell him he can get rid of it but I didn't go further into details, because it will just steer away from his actual question.
you'll need to include stdlib.h if you're planning on using system("pause") or you can try to get rid of it instead.
I think visual studio actually halts the program automatically at the end if I remember it right, but if it doesn't you need to add the system("pause") to halt program execution before closing so you may view the results.
It's not really a good way of doing things though since it's not portable, but maybe that's what they're teaching you in school. If it is, just do what they expect you to do. :P
Anyway, all the different headers still confuse me a bit though, like what's the difference between cstdlib and stdlib.h? Or why should cstdlib be preferrable? I've always used stdlib.h in C programming so I usually end up using that as a habit.