You are on page 1of 5

Data Structure and Algorithm 1

Lab Journal 1
Name Maaz Nafees

Enrollment #:01-235171-074____________

Class/Section: BSIT(3B) _______________

Task1: Give answers to the following.

1. Print the elements of the array ‘a’ using the mentioned notations:
int a[]={1,2,3,4,5};
int *p;
p = a;
for (int i = 0; i < 5; i++)
{

Cout<<a[i];
ss
Cout<<p[i];/

Cout<<*(a+i);

Cout<<*(p+i);

2. Write C++ statement(s) to allocate space for 10 doubles (using dynamic memory
allocation).

double *a=new int[10];


for(int i=0;I<10;i++)
{
cin>>a[i];
}
delete[]a;

3. Study the given program and determine what the program is intended to do. (Hint:
Dry run the program with array {1,2,1,2,3} and analyze the output).

int size = 5;
int *arr = newint[size];
int i, j, k;
Data Structure and Algorithm 2

cout<<"Enter array elements: ";


for (i=0;i<size;i++)
cin>>arr[i];

for (i = 0; i < size; i++) {


for (j = i + 1; j < size;) {
if (arr[j] == arr[i]) {
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
size--;
} else
j++;
}
}

for (i = 0; i < size; i++) {


cout<< arr[i]<<endl;
}

delete arr;

Answer: 123

Task2: Implement the given exercises.

Exercise 1:

Write three C++ functions which accepts an array of integers and the size of the array and
finds:
a. Sum of the elements in the array
b. Average of the array elements
c. Minimum and maximum values in the array

In the main program, declare an array of 10 integers using dynamic memory allocation and
call these threefunctions. Display the output of the function within the main.

QUESTION 1:
Program:
#include <iostream>
 
Data Structure and Algorithm 3

using namespace std;


 
int Sum(int *arr, int arrSize)
{
    int sum = 0;
 
    for(int i = 0; i < arrSize; i++)
    {
        sum = sum + *arr[i];
    }
 
    return sum;
}
 float Avg(int *arr, int arrSize)
{
    int sum = 0;
 
    for(int i = 0; i < arrSize; i++)
    {
        sum = sum + *arr[i];
    }
 
    return sum/10;
}
void max_min(int *arr,int arrSize,int &min,int &max)
{

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


{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}

}
int main()
{
 
int size = 10;
int *arr = newint[size];

        int max = a[0];


int min = a[0];

for(int i=0;I<size;i++)
{
Data Structure and Algorithm 4

cin>>arr[i];
}

 
    cout<<”sum is:”<< Sum(arr,size)<<endl;

cout<<”Average is:”<< Avg(arr,size)<<endl;


 max_min(arr,size,min,max);
cout<<”max value is:”<<max<<endl;
cout<<”min value is:”<<min<<endl;
     
    system(“pause”);
}

Exercise 2:

Write a program with a function which accepts an array of integers and a key value.The
function should return the sum of all the multiples of the key value in the array. For
example, for the array {1, 4, 10, 12, 15, 20, 22} and the key value 5, the function should
return the sum 10+15+20.

QUESTION 2:
Program:
#include<iostream>
#include<conio.h>
using namespace std;
void sumis(int a[],int size,int keyvalue,int &sum);
void main()
{
int arr[8];
int keyvalue;
int sum=0;
for(int i=0;i<8;i++)
{
cin>>arr[i];

}
cout<<"enter the key value: ";
cin>>keyvalue;
sumis(arr,8,keyvalue,sum);
cout<<sum;
system("pause");
}
void sumis(int a[],int size,int key,int &sum)
{
for(int i=0;i<8;i++)
{
if(a[i]%key==0)
{
sum=sum+a[i];
Data Structure and Algorithm 5

}
}}

Implement the given exercises and get them checked by the instructor. If you are unable
to complete the tasks in the lab session, deposit this journal alongwith your programs
(printed or handwritten) before the start of the next lab session.

Submitted to:
Task 1:
Task 2:
Date:
Total marks:

You might also like