Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EXPERIMENT NUMBER – 3.

2
STUDENT’S NAME –SAKSHI KUMARI
STUDENT’S UID – 21BCS9402
CLASS AND GROUP – 217-B
SEMESTER – 02

PROGRAM- 1
WAP to calculate sum of marks of n students of a class inputted via dynamic memory allocation.

ALGORITHM:
1. Start
2. Take the number of students whose marks are to be stored from the user and store it in ‘n’.
3. Dynamically allocate memory of size ‘n’.
4. Input marks.
5. While the marks are being input add them into a variable ‘sum’.
6. Delete allocated memory.
7. Print ‘sum’.
8. End

PROGRAM CODE-
#include <iostream>

using namespace std;

int main(){

cout << "Name: SAKSHI KUMARI" << endl<< "UID: 21BCS9402" << endl;
cout << "**************************" << endl<< endl;

int n,sum=0;

cout<<"Enter the number of students: ";


cin>>n;

int *arr= new int[n];

cout<<"Enter the marks:\n";

for(int i=0;i<n;++i){
cout<<"Marks of student "<<i+1<<": ";
cin>>arr[i];
sum+= arr[i];
}

delete[] arr;

cout<<"The sum of marks of all students is "<<sum;

return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION-

NIL

OUTPUT-
PROGRAM- 2
WAP to allocate memory dynamically for an object of a given class using class’s constructor.

ALGORITHM-
1. Start
2. Create a class which has a manually implemented constructor and destructor that prints
something when objects of the class are created and destroyed.
3. Dynamically allocate memory for the objects of the class.
4. Delete allocated memory.
5. End

PROGRAM CODE-
#include <iostream>

using namespace std;

class Temp{
public:

Temp(){
cout<<"Object created."<<endl;
}

~Temp(){

cout<<"Object deleted."<<endl;
}
};

int main(){

cout << "Name: SAKSHI KUMARI" << endl<< "UID: 21BCS9402" << endl;
cout << "**************************" << endl<< endl;

Temp * objects= new Temp[5];

cout<<endl;

delete[] objects;

return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION-

NIL
OUTPUT-
LEARNING OUTCOMES-

1. Remember the concepts related to fundamentals of C language, draw flowcharts and


write algorithm/pseudocode.

2. Understand the way of execution and debug programs in C language.

3. Apply various constructs, loops, functions to solve mathematical and scientific


problem.

4. Analyze the dynamic behavior of memory by the use of pointers.

5. Design and develop modular programs for real world problems using control structure
and selection structure.

EVALUATION COLUMN (To be filled by concerned faculty only)-

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post-Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre-Lab Questions
4. Total Marks 20

You might also like