array and input in c++ With examples

Image result for c++Going forward with C/C++ programming language tutorial today we will learn how to take input from user when program is running. Lets see an example for this. The program below will ask user to enter a number then it will print whether number is even or odd.
#include <stdio.h>

int
main() {

printf("Please enter a number: ");
int
i = 0;
scanf("%d", &i);

printf("You entered %d \n", i);
if
(i%2 == 0) {
printf("Number is even\n");
}
else {
printf("Number is odd\n");
}


scanf("%d", &i);
}


Run this program and it will print "Please enter a number:" and stop. Type any number and press enter. Then it will print that number you have entered and it will also print if number is even or odd. Now lets see how it works.
After main() 1st line is printf("Please enter a number: "); You are familiar with this. After that variable i is defined Which will hold the number entered by user. See the next line which says scanf("%d", &i);
scanf works similar as printf. Its 1st argument "%d" means same as printf which tells input will be int. 2nd argument &i specifies the variable in which input will be stored. (Here & sign is necessary before variable. We will know the reason when we learn about pointer) If you enter 25 on screen then value of i will be 25. Program after that line you can understand easily with help of previous topics - Here in screen we have printed the number entered by user. Note that % gives the remainder. Hence if diving that number with 2 given 0 remainder then we print that "Number is even" otherwise we print "Number is odd".
Reason to write scanf at the end: If you run program in windows, it shows output in black windows which closes very quickly as program finished and you won't have much time to read the output. If we write scanf in end, window will not close, rather it will wait for input. This will give us enough chance to examine the output.

Array.
We have studied about variable that a variable can store a value but when defining variable we have to tell what type of value store करेगा integer, character etc. Array can store more than one values. If you want to store 100 int, you can store all of them in an array. Lets see by an example how do we do it.
In the example given below we will store squares of 0 to 9 in an array then print all of them. Try running this program.

#include <stdio.h>

int
main() {

int
i = 0;
int
arr[10];

for
(i = 0; i < 10; i++) {
arr[i] = i*i;
}


for
(i = 0; i < 10; i++) {
printf("square of %d is %d\n", i, arr[i]);
}


scanf("%d", &i);
}


In int arr[10]; arr is a variable which can hold 10 integers. Remember it can store only integers from index 0 to 9. Hence in this case 1st integer will be at arr[0], 2nd at arr[1] and so on. Size of arr is 10 hence it will store 10 integers from arr[0] to arr[9]. After that its easy to understand program. Statements inside for loop(which will run 10 times) when we come 1st time, i will be 0 hence 1st integer will be stored at arr[0] will be 0. Similarly going forward inside for loop when we come last time inside for loop, i will be 9 hence arr[9] will be 81. Similarly in next for loop we reading values of array and printing.
Usually we use for loop as shown above to access elements in the array.
In next topic we will learn about while loop.
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.