Sunteți pe pagina 1din 10

ASSIGNMENT # [1]

NAME Izhar Ul Mulk

DEPARTMENT Computer Science

SEMESTER 3rd

SUBJECT Data Structure

SUBMETTED TO Mr. Dost Muhammed

Lecturer in computer science

1
1) C++ program to store and calculate the sum of 4 numbers
entered by the user using arrays.
#include <iostream>
using namespace std;
int main()
{
int numbers[4], sum = 0;
cout << "Enter 4 numbers: ";
for (int i = 0; i < 4; ++i)
{
cin >> numbers[i];
sum = numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
output
Enter 4 numbers: 2

2
4
1
3
sum = 10

2) C++ program to store and calculate the subtraction of 5


numbers entered by the user using arrays.
#include <iostream>
using namespace std;
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sub = numbers[i];
}
cout << "subtraction = " << sub << endl;
return 0;
}
output
Enter 4 numbers: 9

3
2
4
1
sub = 2

3) Find out and print the maximum value in the array.


#include <iostream>

using namespace std;

main()

float xyz[5] , max ;

int i ;

for (i=0; i<=4; i++)

cout<<"Enter values in element i = " ;

cin>>xyz[i];

max = xyz [0];

for (i=1; i<=4; i++)

if (max < xyz[i])

4
max = xyz [i] ;

cout<<"Maximum value is = "<<max;

output

Enter values in element i = 2 , 4, 5, 6, 7

maximum value = 7

4) Printing my name using array


#include <iostream.h>

Using namespace std;

int main()

char str[6] = {'I','Z','H','A','R'};

for (i=0; i<=6; i++){

cout<<str[i];

return 0;

Output

IZHAR

5
5) Access Array Elements Using Pointer
#include <iostream>

using namespace std;

int main()

int data[4];

cout << "Enter elements: ";

for(int i = 0; i < 5; ++i)

cin >> data[i];

cout << "You entered: ";

for(int i = 0; i < 4; ++i)

cout << endl << *(data + i);

return 0;

output

Enter elements: 1,2,3,4

You entered: 1,2,3,4

6) Show Largest Element of an array


#include <iostream>

6
using namespace std;

int main()

int i, n;

float arr[100];

cout << "Enter total number of elements(1 to 100): ";

cin >> n;

cout << endl;

for(i = 0; i < n; ++i)

cout << "Enter Number " << i + 1 << " : ";

cin >> arr[i];

for(i = 1;i < n; ++i)

if(arr[0] < arr[i])

arr[0] = arr[i];

cout << "Largest element = " << arr[0];

return 0;

7
output

Enter total number of elements: 4

Enter Number 1: 44

Enter Number 2: 55

Enter Number 3: 66

Enter Number 4:77

Largest element = 77

7) Calculate Average of Numbers Using Array


#include <iostream>

using namespace std;

int main()

int n, i;

float num[100], sum=0.0, average;

cout << "Enter the numbers of data: ";

cin >> n;

while (n > 100 || n <= 0)

cout << "Error! number should in range of (1 to 100)." << endl;

cout << "Enter the number again: ";

8
cin >> n;

for(i = 0; i < n; ++i)

cout << i + 1 << ". Enter number: ";

cin >> num[i];

sum += num[i];

average = sum / n;

cout << "Average = " << average;

return 0;

output

Enter the numbers of data: 3

1. Enter number: 23

2. Enter number: 12

3. Enter number: 45

8) Findind position of values using array


#include <iostream>

#include<conio.h>

9
using namespace std;

int main()

int i;

int value[3] = {5,10,15};

cout<<"Single Dimensional Array Example \n";

for (i=0;i<3;i++)

cout<<"Position : "<<i<<" , Value : "<< value[i]<<" \n";

return 0;

output

Single Dimensional Array Example

Position : 0 , Value : 5

Position : 1 , Value : 10

Position : 2 , Value : 15.

10