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

Lecture22 25

Here are solutions to the homework problems using arrays: Problem 1: #include <iostream> using namespace std; int main() { char arr[10]; cout << "Enter 10 characters: "; for(int i=0; i<10; i++) { cin >> arr[i]; } cout << "Reversed order: "; for(int i=9; i>=0; i--) { cout << arr[i]; } return 0; } Problem 2: #include <iostream> using namespace std; int main() { int arr[10], max, min; cout << "

Uploaded by

obaidulhaq636
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)
10 views

Lecture22 25

Here are solutions to the homework problems using arrays: Problem 1: #include <iostream> using namespace std; int main() { char arr[10]; cout << "Enter 10 characters: "; for(int i=0; i<10; i++) { cin >> arr[i]; } cout << "Reversed order: "; for(int i=9; i>=0; i--) { cout << arr[i]; } return 0; } Problem 2: #include <iostream> using namespace std; int main() { int arr[10], max, min; cout << "

Uploaded by

obaidulhaq636
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/ 30

Programming Fundamentals

(CS-102)

Instructor: Dr. Junaid Rashid

Department of Computer Science

Air University Islamabad


A&AC-Kamra
Contents

• Arrays
• Definition and Basics of Arrays
• Example
Arrays in C++
Declare Variables -What We Have So for
• Declare 10 integers and print on screen.
• Read 100 numbers from user and find their sum.

void main {
int var1, var2, var3, var4, var5, var6, var7, var8, var9,var10;

• Issue with this type of declaration and usage ?


• Solution?
Arrays- Introduction
• In computer languages we also need to group together data items of
the same type. The most basic mechanism that accomplishes this in
C++ is the array

• We have used variables to store values (single value at a time) in


memory for later reuse. We now explore a means to store multiple
values together as one unit, the array.

• Arrays exist in almost every computer language. Arrays in C++ are


similar to those in other languages, and identical to those in C.

• In Standard C++ the array is not the only way to group elements of
the same type. (vector is another way)
C++ Arrays
• Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
• To declare an array, define the variable type, specify the name of the
array followed by square brackets and specify the number of
elements it should store:
string cars[4];
• We have now declared a variable that holds an array of four strings.
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

• To create an array of three integers, you could write:


• int myNum[3] = {10, 20, 30};
Access the Elements of an Array
• You access an array element by referring to the index number.
• This statement accesses the value of the first element in cars:
• Example

#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
Output:
Volvo
Change an Array Element
• To change the value of a specific element, refer to the index number:
• Example
• cars[0] = "Opel";
• Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}
Output: Opel
Loop Through
You can loop through thean Array
array elements with the for loop.
The follow
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << cars[i] << "\n";
}
return 0;
}
ing example outputs all elements in the cars array:
Output
Volvo
BMW
Ford
Mazda
The following example outputs the index of each element together with its value:
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
return 0;
}
Output
0: Volvo
1: BMW
2: Ford
3: Mazda
Omit Array Size

• You don't have to specify the size of the array. But if you don't, it will
only be as big as the elements that are inserted into it:
• string cars[] = {"Volvo", "BMW", "Ford"}; // size of array is always 3

• This is completely fine. However, the problem arise if you want extra
space for future elements. Then you have to overwrite the existing
values:
• string cars[] = {"Volvo", "BMW", "Ford"};
string cars[] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
• If you specify the size however, the array will reserve the extra space:
• string cars[5] = {"Volvo", "BMW", "Ford"}; // size of array is 5, even
though it's only three elements inside it

• Now you can add a fourth and fifth element without overwriting the
others:
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[5] = {"Volvo", "BMW", "Ford"};
cars[3] = "Mazda";
cars[4] = "Tesla";
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
Volvo
BMW
Ford
Mazda
Tesla
Omit Elements on Declaration

It is also possible to declare an array without specifying the elements


on declaration, and add them later:
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
cars[4] = "Tesla";
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
Output:
Volvo
BMW
Ford
Mazda
Tesla
Arrays- Introduction
• Arrays are fixed-size collections consist of the data items of the same
type.

• An array stored sequentially in memory (contiguous memory location)

• Therefore, an integer array holds some number of integers, a character


array holds some number of characters.

• Array has same properties as for variable in C++ (type, name, size)

Datatype ArryName [Int_expression] Datatype varName


Array declaration Variable declaration
Array- Example
#include<iostream>
using namespace std;

int main (){

int arrayVar1[5];

arrayVar1[0]=10;
arrayVar1[1]=20;
arrayVar1[2]=30;
arrayVar1[3]=40;
arrayVar1[4]=50;

cout<<"\n array 1st element [0] : "<<arrayVar1[0];


cout<<"\n array 2nd element [1] : "<<arrayVar1[1];
cout<<"\n array 3rd element [2] : "<<arrayVar1[2];
cout<<"\n array 4th element [3] : "<<arrayVar1[3];
cout<<"\n array 5th element [4] : "<<arrayVar1[4];

return 0;
}
Array – Example Array subscripting operator

int arrayVar1[5];

Type
int, float, double, char

Size/dimension
Name Must be unsigned integer value
Same rules as for variables
Arrays – Memory View
Name

memory
arrayVar1[0] 10
arrayVar1[1] 20
arrayVar1[2] 30 arrayVar
arrayVar1[3] 40 1

arrayVar1[4] 50

Index
Start from zero to size-1
Declaration of Arrays Datatype ArryName [Int_expression_size]

• Array must be declared before its first use (as for variables)
int arrayVar1 [ 50 ] ; char myChar[20]

• More than one array can be declared on a line


int student[10] , Faculty[30] , Player[20] ;

• Mix declaration of variables with declaration of arrays


int var1 , var2 , marks [8] ;
Initializers

• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
• If too many a syntax error is produced

• If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
Advantage of array

• It is used to represent multiple data items of same type by using only


single name.

• It can be used to implement other data structures like linked lists,


stacks, queues, trees, graphs etc.

• 2D arrays are used to represent matrices.

27
Limitations of array
• We must know in advance that how many elements are to be stored in
array.

• Array is static structure. It means that array is of fixed size. The memory
which is allocated to array can not be increased or reduced.

• The elements of array are stored in consecutive memory locations. So may


be consecutive location is not available in memory so in this scenario
program will create exception.

• The elements of array are stored in consecutive memory locations. So


insertions and deletions are very difficult and time consuming.
Type of Arrays
• One dimension Array (1D)
• Two dimension Array (2D)
• Multi dimensional Array (ND)

B
A 1 D
C

B
2
Homework
Problem#1

Write a program that take 10 characters from user and print it in


reverse order of the input.

Problem#2:
Write a program that take 10 integer from user and find the maximum
and minimum using arrays;.

You might also like