0% found this document useful (0 votes)
12 views4 pages

Assignment 1 - Progamming Fundamental

The document contains programming assignments for a Computer Science course, detailing five tasks related to array manipulation in C++. Each task includes a specific problem statement, example input and output, and the corresponding C++ code to solve the problem.
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)
12 views4 pages

Assignment 1 - Progamming Fundamental

The document contains programming assignments for a Computer Science course, detailing five tasks related to array manipulation in C++. Each task includes a specific problem statement, example input and output, and the corresponding C++ code to solve the problem.
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/ 4

Assignment: Progamming Fundamental

Submitted To: Sir Kaleem SB.


Submitted By: Kamran Fareed
Roll No: 51
Department: Computer Science
Class: BS CS 1st Semester (Evening

Question No.1: Write a program to find the largest and smallest elements in an array.
Example Input: {3, 5, 7, 2, 8}
Output: Largest: 8, Smallest: 2

Answer:
#include<iostream>
using namespace std;
void input (int []);
int maximum (int []);
int minimum (int []);
const int SIZE=5;
int main()
{
int num[SIZE];
input(num);
int n= maximum(num);
cout << "Maximum number is " <<n << endl;
int v=minimum(num);
cout << "Minimum number is "<<v << endl;
return 0;
}
void input (int nums[])
{
cout << "Enter values of elements:" << endl;
for(int i=0;i<SIZE;i++)
{
cout << "Element #"<< i+1 <<" :" << endl;
cin >> nums[i];
}
}

int maximum (int nums[])


{
int max=nums[0];
for(int i=1;i<SIZE;i++)
{
if (nums[i]>max)
max=nums[i];
}
return max;
}
int minimum (int value[])
{
int min=value [0];
for(int i=1;i<SIZE;i++)
{
if (value[i]<min)
min=value[i];
}
return min;
}

Question No.2: Write a program to reverse the elements of an array.


Example Input: {1, 2, 3, 4, 5}
Output: {5, 4, 3, 2, 1}

Answer:
#include<iostream>
using namespace std;

int main()
{
int reverse [10];
cout << "Enter values of elements in arry:" << endl;
for (int i=0;i<10;i++)
{
cout << "Element # "<< i+1 <<" :" << endl;
cin >> reverse[i];}
cout << "values in actual order are: "<<endl;
for (int i=0;i<10;i++)
{
cout <<reverse[i] << " ";

}
cout << "Values in reverse order are:" <<endl;
for (int i=9;i>=0;i--)
{cout << reverse[i] << " ";
}
return 0;
}
Question No.3: Calculate the sum and average of all the elements in an array.
Example Input: {4, 6, 8, 10}
Output: Sum: 28, Average: 7

Answer:
#include<iostream>
using namespace std;

int main()
{
int SIZE=10;
float sum=0,avg;
float arry[SIZE];

cout << "Enter values of elements in arry:" << endl;


for (int i=0;i<SIZE;i++)
{
cout << "Element #"<< i+1<<" :" << endl;
cin>>arry[i];
sum+=arry[i];
}
avg= sum/10.0;
cout << "Sum of numbers is "<< sum << endl;
cout << "Average of numbers is "<<avg << endl;

return 0;
}

Question No.4: Write a program to find whether a given number exists in the array.
Example Input: Array: {10, 20, 30, 40}, Search: 30
Output: Found at position 3

Answer:
#include<iostream>
using namespace std;

int main()
{
int loc = -1, n;
int num[4] = {10, 20, 30, 40};
cout << "Enter number to find:" << endl;
cin >> n;
for (int i = 0; i < 4; i++)
{
if (num[i] == n)
{
loc = i;
break;
}
}
if (loc == -1)
cout << "Number not found..." << endl;
else
cout << "Found at position " << loc + 1 << endl;
return 0;
}

Question No.5: Count how many times a specific number appears in an array.
Example Input: {1, 2, 3, 1, 4, 1}
Search: 1
Output: 3 times

Answer:
#include<iostream>
using namespace std;

int main()
{
int n, count = 0;
int arr[6] = {1, 2, 3, 1, 4, 1};
cout << "Enter number to find how many times it appears in array:" << endl;
cin >> n;
for (int i = 0; i < 6; i++)
{
if (arr[i] == n)
count += 1;
}
cout << n << " appears " << count << " times in array." << endl;
return 0;
}

You might also like