Professional Documents
Culture Documents
Program-1 Algorithm:: Experiment Number - 3.2
Program-1 Algorithm:: Experiment Number - 3.2
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>
int main(){
cout << "Name: SAKSHI KUMARI" << endl<< "UID: 21BCS9402" << endl;
cout << "**************************" << endl<< endl;
int n,sum=0;
for(int i=0;i<n;++i){
cout<<"Marks of student "<<i+1<<": ";
cin>>arr[i];
sum+= arr[i];
}
delete[] arr;
return 0;
}
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>
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;
cout<<endl;
delete[] objects;
return 0;
}
NIL
OUTPUT-
LEARNING OUTCOMES-
5. Design and develop modular programs for real world problems using control structure
and selection structure.