Monday, March 17, 2014

[Tutorial C++] Basics of Programming Part 3

This is the third part in my basics of programming series. In this part I shall tell about arrays, how to declare, initialize and use them. Being a beginner your first question would be "What are arrays?". Well the answer for this is quite easy, arrays are like tables where you can store information for a particular object like names, address, phone number, player stats etc. If we had used variables here to store the data then obviously we had to make many variables and keeping a track of them would have been difficult. Arrays makes that really easy as everything is stored under a single name (with a small change which I will show later)  and accessing them becomes also easy.

So how do we declare an array. Well that's easy too. To declare an array first write its type, then the name you would like to give the array and then the number of data it needs to store but it should be written inside the square brackets. Like I want to store some marks which will be out 10 of students in a class, so I will first write its type that is going to be "int" and then I write the name I would like to give the array which is "Marks" and then I will write the number of data I am going to store, I want to store the marks of five students so I will write "5" but not like this we need to include them in square brackets so it should be like this "[5]" and then a semi-colon to end the statement. The final result would like this.

int Marks[5];
Now that we have done the declaration part we shall go on how to initialize an array and store data in it. Every data we store on array is known at its element. So if we store the first data then it is the first element of the array and when we store the second data it is the second element of the array and that continues till the last element in the array. In the array we have declared we have told that C++ that we want to store the marks of five students so we have declared an array of five elements. There are two ways we can store data an array one is that we store the data at the beginning and the other way is to store the data after declaration we can also use this way to edit the data later in the program. Below I will show you both methods.

int Marks[5]={5, 6, 9 ,3, 7};


int Marks[5];

Now here's the confusing part. You would have seen me starting the elements from zero rather than one. Well in C++ you start the first element of array from zero not one and then count to five so that the fifth element is four. So whenever you are going to refer to first element its going to be zero not one. Okay so let's move on to make a program where we shall input the marks of the students, number of students and then find the total and average marks.

#include<iostream> // Declaring header file

#include<conio.h>  // Declaring header file

using namespace std;

int main() //main function


int marks[5], sum, numofstd; //Declaring an array and then two variables

float average; /*This is a new thing, here I am using a new type to declare a variable as average mostly comes in decimal points so if we have a decimal number we have to use float type rather than the integer type.*/

cout<<"Enter the number of students:"<<endl;//Displaying a message and using endl to end the line

cin>>numofstd;  //taking input from the user

cout<<"Enter the marks of the students:"<<endl;//Displaying a message and using endl to end the line

cin>>marks[0];  //taking input from the user

cin>>marks[1];  //taking input from the user

cin>>marks[2];  //taking input from the user

cin>>marks[3];  //taking input from the user

cin>>marks[4];  //taking input from the user

sum=marks[0]+marks[1]+marks[2]+marks[3]+marks[4];  //calculating sum here

average=(float)sum/numofstd;  /*calulating average here, here I am using a new 
technique called casting. We use this because our variable "sum" was declared in "int" 
form but our "average" variable is in float form and if we want to get the average in 
float form than the variable given to it for finding average should also be in float 
form so to get that we use casting that is place two round brackets before the 
variable whose type you want to change and inside the the brackets write the type you 
want for it. In our case it is "float".*/

cout<<"Sum="<<sum<<endl; //Displaying sum

cout<<"Average="<<average<<endl; //Displaying average



In this program I have used two new things that is a float type variable and using the technique of casting. I hope you got what I tried to taught you and like always if you have any questions please do ask and I will try to anwser them asap. Here's how the program finally looks like when executed.



Copyright @ 2014 CPP Fuzz.