Thread: Accessing variables in a set individually.

1. Accessing variables in a set individually.

Here is a simple program that prints 0 to 9 integers.
Code:
```#include <iostream>
using namespace std;
int main () {
int x;
for (x=0;x<10;x++)
cout<< x;
return 0;
}```
My question is, is there a way in which I can, for example, access these numbers individually and allows me to manipulate them, for example, by multiplying them together or adding or subtracting them from each other and so on. I mean I know one way of getting their total but I don't want to access the numbers as a set but as individual numbers. So for example instead of just getting the total, I want to add 1+3 from these numbers or multiply 2 and 5 and so on. Thanks in advance.

2. It's a bit hard to understand what you mean by accessing numbers. You can have one or more variables each with the value you like. For collections of variables one would use arrays:

Code:
```#include <iostream>

int main()
{
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << array[1] + array[3] << '\n';
}```

3. Originally Posted by anon
It's a bit hard to understand what you mean by accessing numbers. You can have one or more variables each with the value you like. For collections of variables one would use arrays:

Code:
```#include <iostream>

int main()
{
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << array[1] + array[3] << '\n';
}```
Ok, I understand this. But I mean in my example above, for example, we have numbers from 0 to 9. My questions is, is there like a way I can manipulate these numbers individually, by for example dividing 6 by 3 or multiply 2+4 and so on.

4. Code:
`std::cout << 6 / 3 << ' ' << 2 + 4 << '\n';`
I don't really understand your question. You don't have numbers from 0 to 9. You have a variable x whose value is initially 0 and then incremented till it becomes 10.

5. Code:
```#include <iostream>
using namespace std;

int main ()
{
float n;
for (int x=0; x<10; x++)
{
n = x / 6.0;
cout << n << endl;
}
return 0;
}```
This divides each x by 6, is that what you were asking about... ?

In a FOR loop when you want to work with the incrementing variable you may work with a single, 'current', value of that variable at any time- obviously. So if you need to do something else consider a different approach to your problem.

Maybe if you explain what problem you are trying to solve we can offer you better suggestions.

6. Originally Posted by gltiich
Code:
```#include <iostream>
using namespace std;

int main ()
{
float n;
for (int x=0; x<10; x++)
{
n = x / 6.0;
cout << n << endl;
}
return 0;
}```
This divides each x by 6, is that what you were asking about... ?

In a FOR loop when you want to work with the incrementing variable you may work with a single, 'current', value of that variable at any time- obviously. So if you need to do something else consider a different approach to your problem.

Maybe if you explain what problem you are trying to solve we can offer you better suggestions.
This is the problem. I have a code here for example where I am trying to ask a user to put in two numbers and the program will give out all the numbers between the two numbers, and then I find out the average, sum, standard deviation etc. I managed to work out the sum so far but I do not know how I can ask the program to square each of those numbers individually. Here is ma program.

Code:
```#include <iostream>
using namespace std;
int main ()

{
int p,x,y,z;
double average;
do {cout<<"Enter a number\n";
cin>>y;
p=y;
cout<<"Enter the other number\n";
cin>>z;
x=z+1;

double sum=0;
for(;y<=z;y++)
{cout<<y<<",";
sum=sum+y;}
int n;
for (n=y;n<=y;n++)
{
float n;//I am stuck here.
I tried to use your suggestion but
//nothing worked so far.
for (int x=y; x<=y; x++)
{
n = x*x;
cout << n << endl;
}
}
average=sum/(x-p);

cout<<endl<<"Sum is: "<<sum<<endl<<"Average is: "<<average<<endl;
}
while (1);
}```
PS: how can you get a program to terminate only when a user taps a key (for example the key letter F etc or when they press yes, no etc?)

7. You are making some confusions in that code, and doing things you don't really need to.

You want to display the square of all numbers in a range, and calculate their average, so you only really need one loop to do it.

Make a loop that goes from the first number in range, to last number in range. In that loop, add the number to your variable sum, and also print the square of that number. For each loop, you can increment another variable (a counter) to count how many numbers have you added so far.

Since average would be total of all numbers divided by the number of numbers you added, the result would just be sum / your counter variable.

Try to do it with those ideas to see how that goes.

8. OK, after some time, I got it to work but it is different from what you said and perhaps too much. Here is what I did. Now, the problem that remains is that the words in cout are printed every time so it says the square is 1, the square is 4 and so on, repeating it over and over. Is there a way in which I can get rid of these repetitions? Finally, how can I make it possible for the user to terminate the program when they wish by tapping a certain key(s). For example by pressing F or or typing yes/no etc. So far I only know loops as you can see in my example.

Code:
```#include <iostream>
using namespace std;
int main ()

{
int p,x,y,m,v,z;
double average,square;
do {cout<<"Enter a number\n";
cin>>y;
p=y;
v=y;

cout<<"Enter the other number\n";
cin>>z;
x=z+1;

double sum=0;
for(;y<=z;y++)
{cout<<y<<"," ;
sum=sum+y;}
{
float n=y;
int t;
for (v; v<=n; v++)
{
square = v*v;
cout <<endl<<"The square is: "<< square ;//now this message is printed for
// every square and I want it printed ONLY once. How can I do that?
}
}
average=sum/(x-p);

cout<<endl<<"Sum is: "<<sum<<endl<<"Average is: "<<average<<endl;
}
while (1);
}```

9. As Litz said, you are confusing yourself way too much. I don't understand why you are assigning 2 variables to the value of one variable (in total, 3 variables with the same value) instead of using the same one.

I am referring to:
Code:
```    cin>>y;
p=y;
v=y;```
The design of your program is over-complex for the task you are actually trying to complete. All you should do is read both numbers from stdin, sort out which number is higher and which is lower, than, loop the amount of times of the difference between the higher number and the lower number and print the corresponding number through each iteration.

I certainly do not like, nor support spoon-feeding, but since I see you have some difficulties, I will write a prototype of what your application should look like to print all the numbers between two numbers given by the user.

Code:
```#include <iostream>

using namespace std;

int main(int argc, string argv[])
{
while (1) {
int a = 0;
int b = 0;

int high = 0;
int low = 0;

cout << "Enter a number: ";
cin >> a;

cout << "\nEnter the other number: ";
cin >> b;

if (a == b) {
cout << "\nBoth numbers you entered are equal.";
continue;
}

high = (a > b) ? a : b;
low (a > b) ? b : a;

cout << "\nPrinting all the numbers in between " << low << " and " << high << ":\n";

for (int i = low + 1; i <= high; i++) {
cout << i << endl;
}
}
}```