0% found this document useful (0 votes)
22 views7 pages

This Manual Covers: I. One Dimensional Array Ii. Sorting Algorithm Iii. 2 Dimensional Array

This document covers exercises on one-dimensional and two-dimensional arrays in C++. It includes 6 exercises on one-dimensional arrays that involve displaying array elements, accessing elements, calculating average, and sorting arrays. Exercise 7 demonstrates storing user input into a 2D array. The assignment asks to write a function that accepts a 2D array and size and displays the middle row and column elements.

Uploaded by

FaIz Fauzi
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)
22 views7 pages

This Manual Covers: I. One Dimensional Array Ii. Sorting Algorithm Iii. 2 Dimensional Array

This document covers exercises on one-dimensional and two-dimensional arrays in C++. It includes 6 exercises on one-dimensional arrays that involve displaying array elements, accessing elements, calculating average, and sorting arrays. Exercise 7 demonstrates storing user input into a 2D array. The assignment asks to write a function that accepts a 2D array and size and displays the middle row and column elements.

Uploaded by

FaIz Fauzi
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/ 7

Lab 6

This manual covers:


i.
ii.
iii.

One dimensional array


Sorting algorithm
2 dimensional array

Exercise 6.1 -----------------------------------------------------------------Write, compile, build and run the following program. The program shows a simple array
by displaying the array number and its content.
#include <iostream>
int main ()
{
int i;
int array[10] = {1,12,9,4,4,34,23,8,2,7};
for (i=0; i<10 ;i++)
cout <<"array["<<i<<"] ="<< array[i]<<endl;
return 0;
}
Exercise 6.2
Write, compile, build and run the following program. The program shows how to
initiate and access an element in array

#include <iostream>
int main()
{

inta,b,c,d;
inti=1,j=2,k=3;
int array[10] = {12,5,4,23,9,32,12,12,9,22};
a = array[5] + array[6];
b = array[1]*array[3];
c = array[i+5];
d = array[j*k];

cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
cout<<"c ="<<c<<endl;
cout<<"d ="<<d<<endl;
return 0;
}--------------------------------------------------------------Exercise 6.3
Write a program that asks 5 marks from user. Find and print to screen the total and
average of the marks

Exercise 6.4
Write, compile, build and run the following program. The program shows an array as a
function parameter.
#include <iostream>
Double avg_array(int []);

int main ()
{
int array[10];
cout<<"average = "<<avg_array(array)<<endl;
return 0;
}
doubleavg_array(int array[])
{
int i, total=0;
double average;
cout<<"Input data :\n");
for (i=0; i<10 ;i++)
{
Cout<<"Data :"<<i+1;
Cin>>array[i];
total += array[i];
}
average = (double)total/10;
return average;
}

Exercise 6.5
Write a program containing a function to sort a series of data in an ascending order

#include< iostream >


void input(int []);
void sort(int []);
void print(int []);

int main (void)


{
int marks[10];
input(marks);
sort(marks);
print(marks);
}
void input(int marks[])
{
Int i;
for (i=0; i<10 ; i++)
{
Cout<<"input marks #"<<i+1;
Cin>>marks[i];
}
}
void sort(int marks[])
{

inti,j,temp;
for (i=0; i<10 ; i++)
{for (j=i+1; j<10 ; j++)
{if (marks[i] > marks[j])
{
temp = marks[i];
marks[i] = marks[j];
marks[j] = temp;
}
}
}
}

void print(int marks[])


{
inti;
for (i=0; i<10 ; i++)
{
Cout<<marks[i]<<"\t";
}
}
Exercise 6.6
Modify the program in Exercise 6.5 to sort a series of data in a descending order.

Exercise 6.7
Write and run the following program. The program shows how to read a data from user
and store it into 2-dimesional array.
#include< iostream >
int main()
{
int square[3][3];
int row, column;
for (row = 0; row < 3; row++)
for (column = 0; column < 3; column++)
{
Cout<<"Input array [<<row<<][<<column<<];
Cin>>square[row][column];
}
for (row = 0; row < 3; row++)
for (column = 0; column < 3; column++)
{
Cout<<"\n Array [<<row<<][<<column<<]\t<< square[row][column];
}
return 0;
}

LAB ASSIGNMENT 3:
Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements of middle row and the elements of middle column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e. 3x3, 5x5, 7x7 etc...]
Example, if the array contents are
3 5 4
7 6 9
2 1 8
Output through the function should be :
Middle Row: 7 6 9
Middle column: 5 6 1

You might also like