Assignment 1 - Progamming Fundamental
Assignment 1 - Progamming Fundamental
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];
}
}
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];
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;
}