How to create series of input numbers?

Pages: 12
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..................................
Any help, plzzzzzzzzzzzzzzzzzzzz
1. The number of values entered

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

still thinking....

7. The two numbers in descending order

which two numbers it want?



*hope it helps
Last edited on
Thank very much Grax

Thanks a lot for your time to help me out Grax

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>
using namespace std;
int main()
{
	float input = 0,
		number = 0,
		sumpositive = 0,
		average = 0,
		smallestpositive = 0,
		percentage = 0,
		largest = 0;
	
		
}
Last edited on
i can do it with excel in no time.

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.
Last edited on
Finally, i've done first 5 part. Still very confusing how to go through part 6 and 7, and idea is welcome.
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?
about part 7, i'm don't know which 2 number you're going to sort, but this code may help you
1
2
3
4
5
6
7
8
9
10
11
12

//this code will swap num1 and num2 if num1 is greater than num2
if(num1 >  num2) 
{
        int temp = num1;
        num2 = num1;
        num1 = temp;
}

cout << num1 << " "  << num2;

Last edited on
Well, i've figured out my problem because i fully understand the way programming work in different way.

This is my works guys. I dont use array and link, i just use loop for if then, do while, they provide very same result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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
using namespace std;
int main ()
{
	const int  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 
dacaster: system pause is bad there are so many better ways for the same effect. why use the .h stdlib when you can use cstdlib and keep proper syntax
@Aramil

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.
Last edited on
Topic archived. No new replies allowed.
Pages: 12