C++ in simple and easy steps with Explanation

Posted on at


1)

#include <iostream>
using namespace std;

int main()
{
char C[10];
cin>>C;//assume user enters abcdef, explain the output
cout<<C;
return 0;
}
EXPLANATION:

In this programme we have a character type array whose name is C and size is 10.
Since it’s a character type therefore we can input and output it directly……..
Also the function used is int main therefore we use return 0 at the end.
2) What if the line Char C[10] is replaced with Char C [10]={0}, explain the output now.


#include <iostream>
using namespace std;
int main()
{
char C[10]={0};
cin>>C;//assume user enters abcdef, explain the output
cout<<C;
return 0;
}

EXPLANATION 2): As we can see result is same but now the array is initialized with a null character.
In this programme we have a character type array whose name is C and size is 10 initialised with a null character.
Since it’s a character type therefore we can input and output it directly……..
Also the function used is int main therefore we use return 0 at the end.


Experiment 3:

#include <iostream>
using namespace std;

int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
int index = sizeof(distance)/sizeof(double);

cout << "Array members and their values\n"; // Using a for loop to scan an array
for(int i = 0; i < index; ++i)
cout << "Distance : " << i + 1 <<":"<< distance[i] << endl;
return 0;
}
Explanation 3):
In this programme the type of array is double while its name is C.the array is initialized as ({44.14, 720.52, 96.08, 468.78, 6.28}) we declare a variable named index which is actually the size of the array finding by formulae(int index = sizeof(distance)/sizeof(double);) this gives us size of array. After this for loop is used to find the size(number) of array and its corrresponding value.

 

Experiment4)

#include <iostream>
using namespace std;

int main()
{
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int minimum = numbers[0];
int a = 8;

for (int i = 1; i < a; i++)
{

if (numbers[i] < minimum)
minimum = numbers[i];
}

cout << "The lowest member value of the array is "<< minimum << "." << endl;
return 0;
}

Explanation 4:
In this programme an array is initialized whose size is 8. A for loop is used to find out the smallest number in the array.


Experiment 5)
#include <iostream>
using namespace std;

int main ()
{

const int MAX = 10;
int numbers[MAX];
int index;
for (index = 0; index < MAX; index++)
{
cout << "Please enter an integer number: " << endl;
cin>>numbers[index];
// FILL IN Code to store values in the numbers array
}

for (index = MAX - 1; index >= 0; index--)
{
cout<<numbers[index]<<endl;// FILL IN Code to write values of the numbers array to the screen
}
return 0;
}
Explanation:
In this programme an array named numbers is initialized whose size is 10 . in the ist for loop array is populated while in the second for loop the array numbers are displayed.
1)
EXERCISES:
1)

SOURCE CODE:
#include<iostream>
using namespace std;
void main()
{
int const a=10;
int array[a];
for(int i=0; i<a; i++)
{ cout<<"enter number plz"<<endl;
cin>>array[i];
}
for(int j=0; j<a; j++ )
{
for(int k=j+1; k<a; k++)
{ if(array[j]==array[k])
{cout<<"numbers are not unique"<<endl;}
else
cout<<"numbers are unique"<<endl;
break;

}
break;
}
}
Exercise 2:)
Source code:
#include <iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

void main ()
{

char a='b';
int sum=0;
float average;
int const size=3;
int reg [size];
float gpa [size];
for (int i=0; i<size; i++)
{
cout<<"enter registration number"<<endl;
cin>>reg[i];
cout<<"enter gpa plz"<<endl;
cin>>gpa[i];
}
do
{
cout<<"Press 1 for Average GPA of class"<<endl;
cout<<"Press 2 for displaying names of students whose GPA is less than 2"<<endl;
cout<<"Press 3 for displaying name of student whose GPA is highest"<<endl;
cout<<"Press 4 for displaying GPA of a particular student"<<endl;
cout<<"Press 5 for modifying GPA of a particular student"<<endl;
cout<<"Press 6 to exit the application" <<endl;
a=getch();

int index=0; int maxgpa=0; int stdregno=0;
switch(a)
{
case '1': for (i=0; i<size; i++)
{ sum=sum+gpa[i];
average= sum/ size;
}
cout<<"average of the class is"<<average<<endl;
break;
case '2': for(i=0; i<size; i++)
{
if (gpa[i]<2)
{ cout<<"regst # of student with gpa less than 2 is"<<reg[i]<<endl;}
}
break;
case'3':
index=0;
for(i=0; i<size; i++)
{ if (maxgpa<gpa[i])
{ maxgpa=gpa[i]; index=i;}
}
cout<<"registrton number with maximum gpa ="<<reg[index]<<endl;
break;
case'4':
index=0;
cout<<"enter reg num of the student you want to know gpa"<<endl;
cin>>stdregno;
for(i=0; i<size; i++)
{
if(stdregno==reg[i])
{
index=i;break;
}
}
cout<<"gpa of student with reg number "<<stdregno<<" = "<<gpa[index]<<endl;
break;
case '5':
index=0; stdregno=0;
cout<<"enter reg num of the student you want to modify gpa"<<endl;
cin>>stdregno;
for(i=0; i<size; i++)
{
if(stdregno==reg[i])
{
cout<<"Enter GPA"<<endl;
cin>>gpa[i];
break;
}
}
cout<<"Modified gpa of student with reg number "<<stdregno<<" = "<<gpa[i]<<endl;
break;
default:
cout<<"You entered wrong choice"<<endl;


}
}while(a!='6');

}

Exercise 3)
Source code:
#include<iostream>
using namespace std;
void main()
{ char f='g';
int count=0;
int const a=5;
int array1[a],array2[a];
cout<<"populate ist array plz"<<endl;
for(int i=0; i<a; i++)
{
cin>>array1[i];
}
cout<<"populate second array plz"<<endl;
for(int j=0; j<a; j++)
{ cin>>array2[j];
}
for(i=0; i<a; i++)
{
for(j=0; j<a; j++)
{ if (array1[i]==array2[j])
{ array2[j]='a';
count++;
break;
}
}
}
if(count==a)

cout<<"arrays are same"<<endl;
else
cout<<"arrays are not same"<<endl;
}

 



About the author

Saif-Filmannex

I am doing Bs Electronics Engineering from International Islamic University

Subscribe 0
160