Computer Science  SLO Based Key Point Notes

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 5: Arrays and String


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:

        int myArray[6];
        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;;
        }



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.


************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************


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

Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.