0% found this document useful (0 votes)
5 views

array

The document provides an introduction to arrays in C++, covering topics such as declaration, initialization, accessing elements, and basic operations. It includes example code snippets for counting passing students, summing array elements, and manipulating array values. Additionally, it discusses advantages and disadvantages of arrays, along with common functions for string manipulation.

Uploaded by

2023828024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

array

The document provides an introduction to arrays in C++, covering topics such as declaration, initialization, accessing elements, and basic operations. It includes example code snippets for counting passing students, summing array elements, and manipulating array values. Additionally, it discusses advantages and disadvantages of arrays, along with common functions for string manipulation.

Uploaded by

2023828024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Prepared by : Nurbaity Sabri

✓ 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 ]

//total data in the array


Set total with 0
Set index with 0
Repeat arraySize times
input num[index]
total = total + num[index]
increase index with 1
endRepeat
Display “ The total = “ , total
End
Find the output using 3, 4,12, 5, 6 :

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.

Example : store group of student matrics numbers.


Advantage Disadvantage

Easier to declare and use Fixed size data. If number of


element stored is less than
max size, the rest of memory
space goes waste.

Can be used with all data If number of elements to be


types. stored more than the max
size, the array cannot
accommodate those new
values.
 arrays must be declared before it can be used
 Syntax :
int money [100]

Number of
Elements

Data type
Array name
 A fixed amount of memory must be allocated
for them :
example :

int age[3]; //to store age of 3 persons


float salary[10]; //to store 10 salaries
char letter[5]; // to store 5 letters
Array can be initialize using same concept of
variable initialization.
Example :
int foo[5] = { 1,8,3,6,12}; //five value in foo
double d[2] = { 0.707, 0.707}; // two value in d
char s[ ] = { 'R', 'P', 'I' }; //using empty declaration it
will used numbers of element insert.
The statement:

int list[5] = {0}; 0 0 0 0 0

declares list to be an array of 5 components and initializes all of them to zero

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

To access value in array simply using example below :

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

 To replace the value 30 in the third elements of numbers with


value 25.

numbers[2] = 25;

numbers [0] numbers [1] numbers [2]

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;

numbers [a] = 75;

b = numbers [a +2];

numbers [1] = numbers [0] + numbers [2];


No assignment
Use a loop to copy elements from one array to another.
(no direct assignment operation)

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];

age = number; //not allowed in array


Use a loop to compare elements of two
arrays.(no direct comparison between arrays)

Example :
int number[5];
int age[5];

if(number == age) // comparison is not allowed in


array
 Use a loop to perform arithmetic operations
between two arrays.

Example :
int number[5];
int age[5];

sum = number+age; //no direct arithmetic operation


allowed
Assigns the letters x, y, and z to the letters array, replacing the letters A, B, C

char letters [3]={‘A’, ‘B’, ‘C’}; //line 1

letters [0] = ‘x’ //line 2


letters [1] = ‘y’ //line 3
letters [2] = ‘z’ //line 4
Assigns the name Helen to the second element in the names array, replacing
the name Nancy

string names [4] = {“barb”, “Nancy”, “Bill”, “Samuel”}


names [1] = “Helen”

Assigns the empty string to each element in the states array

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];

Q1: Find the summation of even number in


array sales?
Q2: Find the summation of odd index in
array sales?
Q3: Find the average of all values in
sales?
Q1:
int i;
double average, sum = 0;
int sales[10];

for(i = 0; i < 10; i++)


{
cin>>sales[i];
if(sales[i]%2==0)
sum = sum+sales[i];

}
cout<<sum;
}
Q2:
int i;
double average, sum = 0;
int sales[10];

for(i = 0; i < 10; i++)


{
cin>>sales[i];
if(i%2==1)
{
sum = sum+sales[i];
}
}
cout<<average;
Q3:
int i;
double average, sum = 0;
double sales[10];

for(i = 0; i < 10; i++)


{
cin>>sales[i];
sum = sum+sales[i];
}
average=sum/10;
cout<<“average: ”<<average;
 Use a simple loops to count element:

int i;
int count = 0;
for(i = 0; i < 10; i++)
count = count+1;

Q1: Count even number in array sales?


Q2: Count number highest than 3?
 Use a simple loops find min and
max elements: cout<<"MIN : "<<min<<endl;
int i=0; cout<<"MAX : "<<max<<endl;
int sales[10];
double max= 0; Q1: Find highest mark from 3
double min = 0; marks entered?
cin>>sales[i];
min=sales[i]; Q2: From the highest mark divide
with number entered and times
max=sales[i]; with 100?
for(i = 1; i < 10; i++)
{
cin>>sales[i];
if(sales[i]<min)
min = sales[i];
if(sales[i]>max)
max = sales[i];
}
Use a simple loops to search element in array: Find the element input by the
user. If element exist in array sales, display “found”. If not, display, “not found”.

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";

Q1: Display found message if name exist in a list of


student name, else display not found?
Q1:
int i, count=0;
string no="";
string sales[]={"nora","siti"};
cin>>no;
for(i = 0; i<2; i++){
//function returns 0 if strings are equal hence the '!'
if(!no.compare(sales[i]))
count++;
}
if(count==0)
cout<<"not found";
else
cout<<"found";
Commonly used functions

NAME DESCRIPTION
strcmp(s1, s2) Compares one string to
another
strcpy(s1, s2) Copies one string to another

strlen(s) Calculates the length of a


string
strcat(s1, s2) Appends one string to
another
void main()
{
char firstName[30], secName[30];
cout <<“Enter full name: ”<< endl;
cin.getline(firstName, 30);
cout <<“Enter another full name: ”<<endl;
cin.getline(secName, 30);

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;
}

You might also like