0% found this document useful (0 votes)
43 views6 pages

DSA Lab 1

Uploaded by

Mohib Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

DSA Lab 1

Uploaded by

Mohib Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

TASK#01:

Write a Program to enter 10 integers in a single-dimension array and then print out the array in
ascending order.

CODE:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int arr[10];
//Input Array Contents
cout << "Enter The Array Contents:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
// Printing Array Elements In Ascending Order
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
// Array Sorting Algorithm
int temp = 0;
for (int i = 0; i <10-1; i++)
{
for (int j = 0; j < 10 - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//Sorted Array:
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cin.get();
}

Made By: Mohib ---> 107


Time Complexity of The Algorithm:
Best Case → O(n)

Average Case → O(𝑛2 )


Output Snippet:

TASK#02:

Write a Program to enter three integers and output the smallest integer using IF

CODE:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int num1, num2, num3;
int flag=0;
cout << "Enter Three Numbers"<<endl;
cin >> num1 >> num2 >> num3;
if (num1 > num2)
{
if (num1 > num3)
{
flag = num1;
}
else
{
flag = num3;
}
}

Made By: Mohib ---> 107


if (num2 > num1)
{
if (num2 > num3)
{
flag = num2;
}
else
{
flag = num3;
}
}
cout << "The Greatest Number Is: "<<flag;
}

Output Snippet:

TASK#03:

Write a program to create structure named student. Take information of student from user as input
(StdID, StdName, StdAge etc.) Display the output

CODE:

#include <iostream>
#include <string>
using namespace std;

struct Student
{
int StdRollNo=0;
string StdName;
int StdAge=0;
int VoteEligiblity()
{
if (StdAge >= 18)
{
cout << "Student Is Eligible To Vote";
}
else
{
cout << "Student Is Not Eligible To Vote";

Made By: Mohib ---> 107


}
return 0;
}
};
int main()
{
int count = 0;
Student s1[10];

while (true)
{
cout << "\n1-Enter Information\n2-Display Information\n3-Exit";
int selection;
cin >> selection;
if (selection == 1)
{
cout << "\nEnter The Student Data" << endl;
cout << endl;
cin >> s1[count].StdName;
cin >> s1[count].StdRollNo;
cin >> s1[count].StdAge;

}
if (selection == 2)
{
system("CLS");
for (int i = 0; i < count; i++)
{
cout << "\nName:"<< s1[i].StdName;
cout <<"\nAge"<< s1[i].StdAge;
cout << "\nRoll No:" << s1[i].StdRollNo << endl;
s1[i].VoteEligiblity();
cout << endl;
}
}
count++;
}

Made By: Mohib ---> 107


OUTPUT SNIPPETS:

TASK#04:
Implement The Above Task Using Pointers

CODE:
#include <iostream>
#include <string>
using namespace std;

struct Student
{
int StdRollNo=0;
string StdName;
int StdAge=0;
int VoteEligiblity()
{
if (StdAge >= 18)
{
cout << "Student Is Eligible To Vote";
}
else
{
cout << "Student Is Not Eligible To Vote";
}
return 0;
}

Made By: Mohib ---> 107


};
int main()
{
int count = 0;
Student *s1[10],d[10];
s1[count] = &d[count];

while (true)
{
cout << "\n1-Enter Information\n2-Display Information\n3-Exit"<<endl;
int selection;
cin >> selection;
if (selection == 1)
{
cout << "\nEnter The Student Data" << endl;
cout << endl;
cin >> (*s1)[count].StdName;
cin >> (*s1)[count].StdRollNo;
cin >> (*s1)[count].StdAge;
count++;

}
if (selection == 2)
{
system("CLS");
for (int i = 0; i < count; i++)
{
cout << "\nName:"<< (*s1)[i].StdName;
cout <<"\nAge"<< (*s1)[i].StdAge;
cout << "\nRoll No:" << (*s1)[i].StdRollNo << endl;
(*s1)[i].VoteEligiblity();
cout << endl;
}
}
}
}
OUTPUT SNIPPET:

Made By: Mohib ---> 107

You might also like