Professional Documents
Culture Documents
Lab Journal 1
Name Maaz Nafees
Enrollment #:01-235171-074____________
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).
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
delete arr;
Answer: 123
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
}
int main()
{
int size = 10;
int *arr = newint[size];
for(int i=0;I<size;i++)
{
Data Structure and Algorithm 4
cin>>arr[i];
}
cout<<”sum is:”<< Sum(arr,size)<<endl;
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: