array
array
✓ Introduction to array
✓ Array declaration and initialization
✓ Input values into array
✓ Accessing elements of an array
Input 100 marks. Count the number of
students pass. Passing mark is 50.
#include<iostream.h>
main()
{
int count = 0;
int j = 0;
double mark;
while (j < 100)
{
cout<<“Enter mark “;
cin>>mark;
if (mark >= 50)
count++;
j++;
}
cout<<“The number of students who pass the test: “<<count;
return 0;
}
#include <iostream.h>
main()
{
for (int r=0; r < 5; r++)
{
for (int c = 0; c< 5; c++)
{
if (r == c || (r + c) == 4)
cout<<"#";
else
cout<<" ";
}
cout<<"\n";
}
return 0;
}
# #
##
#
##
# #
Convert below algorithm to c++ code.
Start
arraySize = 5
num[ arraySize ]
int size=5;
int num[size];
int total = 0;
int index = 0;
for(int i=0; i<size; i++)
{
cin>>num[i];
total=total+num[i];
}
cout<<"total="<<total<<endl;
30
What is array ?
Collection of variables or object which have the same data
type.
Number of
Elements
Data type
Array name
A fixed amount of memory must be allocated
for them :
example :
8 5 12 0 0
int list[5] = {8, 5, 12};
declares list to be an array of 10 components, initializes list[0] to 8, list[1] to 5,
list[2] to 12 and all other components are initialized to 0
The values of an array can be accessed by using the position of
the stored value
marks[0]=10;
marks[1]=50;
marks[5]=77;
C++:
cout<<marks[0]<<marks[1]<<marks[5];
Output :
10 50 77
numbers [0] numbers [1] numbers [2]
numbers 10 20 30
numbers[2] = 25;
numbers 10 20 25
To pass the value of the third element of
numbers to a variable called a, we could
write:
a = numbers[2] ;
Some other valid operation with arrays:
numbers[0] = 20;
b = numbers [a +2];
No comparisons
Use a loop to compare elements of two arrays.
(no direct comparison between arrays)
No arithmetic operations
Use a loop to perform arithmetic operations between two
arrays.
(no direct arithmetic operations between arrays)
Use a loop to copy elements from one array to
another (no direct assignment operation)
Example :
int number[5];
int age[5];
Example :
int number[5];
int age[5];
Example :
int number[5];
int age[5];
int sub = 0;
while(sub<4)
{
states[sub] = “ ”;
sub = sub + 1;
}
Assigns the squares of the numbers from 1 through 3 to the nums array,
replacing the data stored in the arrray.
for(int x = 1; x<=3; x = x + 1)
nums[x - 1] = x*x;
Assigns the values entered by the user in the prices array, replacing data
stored in the array
for(int x = 1; x<5; x = x + 1)
{
cout<<“enter price:”;
cin>>prices[x];
}
Total or Sum & Average
Find summation of values in array
Use a simple loop to add together array elements:
int i;
double average, sum = 0;
for(i = 0; i < 10; i++)
sum = sum+sales[i];
}
cout<<sum;
}
Q2:
int i;
double average, sum = 0;
int sales[10];
int i;
int count = 0;
for(i = 0; i < 10; i++)
count = count+1;
int i, count=0;
int no;
int sales[]={1,2,3,4,5,6,7,8,9,10};
cin>>no;
for(i = 0; i < 10; i++){
if(no==sales[i])
count++;
}
if(count==0)
cout<<"not found";
else
cout<<"found";
NAME DESCRIPTION
strcmp(s1, s2) Compares one string to
another
strcpy(s1, s2) Copies one string to another
if (strcmp(firstName, secName) == 0)
cout <<“The names are the same”<<endl;
else
cout <<“The names are different”<<endl;
strcpy(firstName,“Marissa”);
cout <<“First name is now ”<<firstName<< endl;
cout <<“The length of the first name is ”
<<strlen(firstName)<<endl;
strcat(firstName,“Nurdia”);
cout <<“First name is now ”<<firstName<< endl;
}