Fundamentals of Programming Lab Journal - Lab # 6: Objective

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Lab 6

Fundamentals of Programming

Lab Journal - Lab # 6


Name: Muhammad Abbas Azam

Enrollment #: 01-235181-029

Class: BS(IT)-1A

Objective

This is lab will cover

 Introduction to 1D arrays.
 Array input/output and array operations.
 2D arrays
 Sorting

Task 1 :

Give answers to the following. Write only the coding statement which is asked not the whole
program.

1. Declare an array of 5 double and assign them any values.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
double arr[] = { 1.2, 3.4, 6.2, 1.9, 7.8 };
_getch();
return 0;
}

2. Assign a value of 6.5 to the 3rd element of the array declared above. Remember first index of array is zero.

#include<iostream>
#include<conio.h>
using namespace std;
int main()

Computer Programming Lab


Page 1
Lab 6

{
double arr[] = { 1.2, 3.4, 6.5, 1.9, 7.8 };
_getch();
return 0;
}

3. Display the first value of the array in question 1.

4. Using a for loop display the values of the array in question 1.

Computer Programming Lab


Page 2
Lab 6

5. Using a for loop take all the values of the array in question 1 from user and then display them using another
for loop.

Computer Programming Lab


Page 3
Lab 6

Task 2 :

Understand the following code fragments and write their output.

1.
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };

cout << "Element \t Value" << endl;

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

cout << i << “\t\t” << n[ i ] << endl;

Computer Programming Lab


Page 4
Lab 6

Output:

2. const int arraySize = 10;

int s[ arraySize ];

for ( int i = 0; i < arraySize; i++ )

Computer Programming Lab


Page 5
Lab 6

s[ i ] = 2 + 2 * i;

for ( int j = 0; j < arraySize; j++ )

cout << “Value at index “<< j <<“ is “ << s[ j ] << endl;

Output:

3. int billy [] = {10, 20, 30, 40, 50};

Computer Programming Lab


Page 6
Lab 6

int n, result=0;

for ( n=0 ; n<5 ; n++ )


{
result = result + billy[n];
}
cout << result;

Write in one line what is happening in above code fragment.

Adding the values of all index of an array

Output:

Computer Programming Lab


Page 7
Lab 6

Exercise 1

Declare an array of 10 integers and get user input to fill the array values. Then, find
the minimum values in the arrays.

Exercise 2

Write a program where you declare 3 arrays of 5 integers each. For two of the
arrays get the values from the user. Add the respective elements of these two
arrays and store the results in the third array.

Example:

Array1= 1 2 3 4 5

Array2 = 1 1 2 2 4

Array3 should be: 2 3 5 6 9

Ar
Computer Programming Lab
Page 8
Lab 6

Exercise 3

Declare a 2D array of 3 rows and 6 columns. Initialize it at the time of declaration.


Change values of 2nd column 3rd row and 5th column 1st row. Assign these two
indexes with any value of your own choice. Display this array using nested for
loops.

OUTPUT :

Exercise 4

Write a program to input value in an array of type integer and size 10. Find out the
Computer Programming Lab
total number of even and odd values entered in the array.
Page 9
Lab 6

Exercise 5

Write a program to input value in an array of type float and size 10. Find out the
average of all the values entered.

Formula for average = sum of all values / total number of values

OUTPUT :

Computer Programming Lab


Page 10
Lab 6

Exercise 6
Write a C++ program that declares a 1D array of size 20 and assign values to it.
Apply Bubble sorting on this array and sort it in ascending order.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[20];
for (int i = 0; i < 20; i++)
{
cout << "Enter the value in index " << i << " =";
cin >> a[i];
}
cout << endl;
cout << "Values in Array before sorting (Acending)" << endl;
for (int j = 0; j < 20; j++)
{
cout << "value in index " << j << " is =" << a[j] << endl;
}
int dell;
for (int k = 0; k < 19; k++)
{
for (int l = 0; l < 19; l++)
{
if (a[l] > a[l + 1])
{

Computer Programming Lab


Page 11
Lab 6

dell = a[l];
a[l] = a[l + 1];
a[l + 1] = dell;
}
}
}
cout << endl;
cout << "Values in Array After sorting (Acending)" << endl;
for (int m = 0; m < 20; m++)
{
cout << "Value in index " << m << " is " << a[m] << endl;
}

_getch();
return 0;
}

Computer Programming Lab


Page 12
Lab 6

Computer Programming Lab


Page 13
Lab 6

Home Task

Write a C++ program that declares an array of size 20 and assign values to it. Then
it asks the user “Enter the number to search in this array”. Then search the number
entered by user in the array and display the index on which it was found. (This task
is similar to exercise 1. Think like that.)

+++++++++++++++++++++++++

Computer Programming Lab


Page 14

You might also like