12th Class (HSSC-II) Computer SLO Based Key Point Notes
(National Book Foundation - As Federal Textbook Board, Islamabad Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
(National Book Foundation - As Federal Textbook Board, Islamabad
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
{Contact WhatsApp # 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}
************************************
************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
Unit 5: Arrays and String
5,1 Introduction to Arrays:
- Sometimes we need to group together different data items/values of the same data type OR
- Sometimes it is necessary to process a collection of values that are related in some way, for example, a list of student marks or a collection of measurements resulting from some experiment.
- Processing such collections of data using only simple variables can be extremely difficult.
- This mechanism is achieved in C using Arrays.
5.1.1. Comcept of an Array:
- An array is composed/group of variables (or elements) of the same data type stored in a consecutive memory location which can be referenced through the same variable name (or array name)
- Each data element or variable of the array has an associated index number (or subscript number).
- To refer to an individual data element/item of the array, we use subscripts, the number in the brackets following the array name.
- The number specifies the element’s position in an array. In C, the numbering is started from 0. For example marks[4] = 83; stores the value 83 in the fifth location of the array.
Example: An array of integer data types that group together 6 variables which store the marks of 6 six different students.
5.1.2. Declaring an Array
An array, in C++, has the following syntax:data_type array_name[Array size];
Note:
- The data_type is the variable data type whether it is float, int, long int, char, etc.
- The variable_name is whatever you want to call it. The size is the number of elements in the array.
Example:
int marks[10];float exp_result[100]; etc.
5.1.3. Initialization of Array
- To initialize an array, the initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example:
- If the number of values supplied are less than the actual number of item in the list then extra spaces in the array will be filled in with zeroes.
- If the number of values supplied is greater than the actual size of the array then the compiler will give an error message.
- If the size of the array is not supplied, the compiler will count the number of items in the initialization list and fix that as the array size. For example:
5.1.4. Using Arrays in Programs:
To perform any operation on the arrays like entering data, printing the data values, searching for the data, comparing the data, sorting the data, etc. a loop should be used.
For example to fill an array using the cin function:
for(int i=0; i<=5; i++)
{
cout<<“Please enter the value: “;
cin>>myArray[i];
}
To print the data values from an array:
for (int i=0; i<=5; i++)
{
cout<<myArray[i]<<endl;;
}
{
cout<<myArray[i]<<endl;;
}
Example # 1
Write a program that reads 5 integer values in an array and finds their total and average.#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5], sum = 0;
float avg;
for(int i=0; i<=4; i++)
{
cout<<“Enter a number: “;
cin>>a[i];
}
for(int j=0; j<=4; j++)
sum = sum + a[j];
avg = sum/5;
cout<<“\n Sum of all numbers is: “<<sum;
cout<<“\n Average = “<<avg;
getch();
}
Example # 2:
Write a program that reads ten integer data values and prints the largest data values?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], max;
for(int i=0; i<=9; i++)
{
cout<<“Enter a number: “;
cin>>arr[i];
}
max = arr[0];
for(int j=0; j<=9; j++)
{
if(max < a[j])
max = a[j];
}
cout<<“The largest value is: “<<max;
getch();
}
Exercise Problem:
Problem ii:Write a program that reads ten numbers in any array and prints them in reverse.
Problem iii:
Write a program that reads ten numbers and prints the smallest along with its index?
Problem iv:
For the given array:
int a[15]={4,8,5,1,3,5,0,12,5,7,3,15,8,4,11}
Write a program that prints the number of times the number 5 appears in the array.
5.2.1. Introduction of Two-Dimensional Arrays
- A two-dimensional array uses a single variable name to represent a collection of the same type of data in a table form or matrix form.
- It has two dimensions i.e. vertical and horizontal dimensions.
- Vertical dimensions represent columns and horizontal dimensions represent rows.
5.2.2. Defining and Initializing a Two-Dimensional Array:
Defining a 2D array
To define a 2D Araay in C++, the type of the elements, the name of the array, and the number of elements it is required to store in rows and columns is mentioned in the declaration statement. Following is the general syntax of the declaration of the 2D array in C++: data_type Array_name[Row size][column size];
For example: int table [5][2];
To specify any element in a 2D array, we need the array name followed by row no. and column no.
Initializing 2D Array
A 2D array can be initialized by assigning values row-wise as given below:int table[3][2] = { { 2, 3 }, { 34, 1 }, { 3, 31} };
Nested braces are used for assigning values row-wise.
5.2.3. Accessing and Filling a 2D array
2D arrays are generally accessed row by row using a nested loop. The row index is used as the outer loop variable and the column index as the inner loop variable.For example to fill an array using the cin function:
int demo[3][2];
for( i=0;i<3;i++ )
{
for( j=0;j<2;j++ )
{
cin>>demo[ i ][ j];
}
}
for( i=0;i<3;i++ )
{
for( j=0;j<2;j++ )
{
cin>>demo[ i ][ j];
}
}
Example 1:
• Write a program that reads integer values in an array of 3 rows and 4 columns and finds the sum of all the values.#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int k[3][4], sum=0;
for(int i=0; i<=2; i++)
{
for(int j=0; j<=3; j++)
{
out<<“Enter a number: “;
cin>>k[i][j];
}
}
for(int a=0; a<=2; a++)
{
for(int b=0; b<=3; b++)
sum = sum + k[a][b];
}
cout<<“Sum of all values is: “<<sum<<endl;
getch();
}
#include <conio.h>
void main()
{
clrscr();
int k[3][4], sum=0;
for(int i=0; i<=2; i++)
{
for(int j=0; j<=3; j++)
{
out<<“Enter a number: “;
cin>>k[i][j];
}
}
for(int a=0; a<=2; a++)
{
for(int b=0; b<=3; b++)
sum = sum + k[a][b];
}
cout<<“Sum of all values is: “<<sum<<endl;
getch();
}
5.3.1 Introduction to Strings
- A sequence of characters is called a string.
- In C++, strings are stored in a one-dimensional array of char data types. It is used to store words, names, addresses, etc.
- The last character of a string is the character “\0” which is called a null character. It is an escape sequence that tells the compiler where the string is end.
- It is automatically appended at the end of the string.
- As the “\0” character shows the end of a string, this means that the size of a string will be one less than the size of the array.
5.3.2 Defining (declaration) a String
To declare a string in C++, the data type char, the name of the string, and the size of the string is mentioned in the declaration statement. i.e.:
char sting_name[ string size ]; For example:
char name[20];5.3.2 initializing a String
To initialize a string, a string constant enclosed by double quotes is provided at the time of string declaration. i.e.:
char sting_name[ string size ] = “string value”;
char sting_name[ string size ] = “string value”;
For example:
char name[20] = “Zahid Hussain”;
char city[] = “Karachi”;
char name[20] = “Zahid Hussain”;
char city[] = “Karachi”;
5.4 Commonly used String Function
1. cin.get() Function:
- This function is used to read a string from the keyboard that may contain blank spaces.
- The cin statement can also be used to read a string but it has the limitation that it considers a blank space as a terminating character. It can only read a string consisting of a single word. Anything typed after a space is ignored.
The syntax of cin.get():
cin.get( str_var, str_size );It has two arguments. Str_var is the name of the string and str_size is the maximum size of the string or array.
EXAMPLE
#include <iostream.h>#include <conio.h>
#include <string.h>
void main()
{
clarscr();
char name[30];
cout<<“Enter your Name: “;
cin.get ( name, 30);
cout<<“You Typed: “<<name;
getch();
}
2. strcpy() Function
This function is used to copy the contents of one string variable to another string variable.Syntax:
strcpy( string1 , string2 );EXAMPLE
#include <iostream.h>#include <conio.h>
#include <string.h>
void main()
{
clarscr();
char str1[10] = “PAKISTAN”, str2[10];
strcpy( str2, str1 );
cout<<str2;
strcpy ( str2, “ZINDABAD”);
cout<<str2;
getch();
}
3. strcat() Function
It is used for concatenation or joining two strings.The general syntax is:
strcat( str1, str2 );EXAMPLE
#include <iostream.h>#include <conio.h>
#include <string.h>
void main()
{
clarscr();
char str1[10] = “Home”, str2[10] = “Work”;
strcat(str1, str2);
cout<<str1;
getch();
}
4. strlen() Function
• This function is used to return the length (the number of characters) of a string.The general syntax is:
strlen(string variable);EXAMPLE
#include <iostream.h>#include <conio.h>
#include <string.h>
void main()
{
clarscr();
char name[30] = “Zahid Hussain”;
cout<<the number of characters are: “<<strlen(name);
getch();
}
5. strcmp() Function
strcmp(string1, string2):
This function is used to compare two strings and return an integer value based on the comparison. i.e.EXAMPLE
#include <iostream.h>#include <conio.h>
#include <string.h>
void main()
{
clarscr();
char str1[10] = “mango”, str2[10]= “potato”, str3[10] = “mango”;
int x;
x= strcmp( str1, str2);
cout<<x<<endl;
x= strcmp( str3, str1);
cout<<x<<endl;
getch();
}
************************************👉👉👉 Computer Science 12th Class Notes (Main Page)
************************************
Shortcut Links For:
1. 5th Class All Subjects Notes
2. 8th Class All Subjects Notes
3. Easy English Grammar Notes
1. Website for School and College Level Physics 2. Website for School and College Level Mathematics 3. Website for Single National Curriculum Pakistan - All Subjects Notes
© 2023 & onwards Academic Skills and Knowledge (ASK)
Note: Write me in the comments box below for any query and also Share this information with your class-fellows and friends.
1. Website for School and College Level Physics
2. Website for School and College Level Mathematics
3. Website for Single National Curriculum Pakistan - All Subjects Notes
0 Comments
Start Tuition (in Pakistan or Abroad) from Experienced Online Teachers.
Contact WhatsApp for tutoring and Assignment help on +923339719149