DSA Lab 1
DSA Lab 1
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();
}
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;
}
}
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";
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++;
}
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;
}
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: