0% found this document useful (0 votes)
38 views10 pages

Assignment 4

This document contains C++ code to implement a number system using arrays and functions. It defines functions to add new numbers, display all numbers, find the frequency of a number, find and replace a number, calculate the average of all numbers, find the largest number, and calculate the sum of all odd numbers. The main function initializes an array, calls an interface function in a loop to get user input, and calls the appropriate function to perform the requested operation until the user chooses to exit.

Uploaded by

Nimra Shahid
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)
38 views10 pages

Assignment 4

This document contains C++ code to implement a number system using arrays and functions. It defines functions to add new numbers, display all numbers, find the frequency of a number, find and replace a number, calculate the average of all numbers, find the largest number, and calculate the sum of all odd numbers. The main function initializes an array, calls an interface function in a loop to get user input, and calls the appropriate function to perform the requested operation until the user chooses to exit.

Uploaded by

Nimra Shahid
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/ 10

NUMERICAL ANALYSIS LAB-06

Assignment No. 04
QUESTION:
Implement number system with arrays and functions.

SOLUTION:
#include<iostream>
using namespace std;
int interface()
{
cout<<endl;
cout<<" ************************************"<<endl;
int opt;
cout<<" 1.Add New Number"<<endl;
cout<<" 2.Print All Numbers"<<endl;
cout<<" 3.Find Frequency of Number"<<endl;
cout<<" 4.Find and Replace Number"<<endl;
cout<<" 5.Print the Average of all Numbers"<<endl;
cout<<" 6.Print the Largest of the Numbers."<<endl;
cout<<" 7.Print the Sum of the Odds."<<endl;
cout<<" 8.Exit"<<endl;
cout<<"Enter Your Opition:";
cin>>opt;
cout<<endl;
return opt;

}
void AddNewNumber(int arr[],int &n)
{
cout<<"Enter Number:";
cin>>arr[n];
n++;
cout<<"Number Add Successfully"<<endl;
}
void DisplayAllNumbers(int arr[],int n)
{
cout<<"All Numbers of Array:";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void FrequencyOfNumber(int arr[],int n)
{
int num,numFreq=0;
cout<<"Enter Number:";
cin>>num;
for(int i=0;i<n;i++)
{
if(arr[i]==num)
{
numFreq++;
}
}
cout<<"Frequecy of "<<num<<" in Array is "<<numFreq<<endl;
}
void FindAndReplace(int arr[],int n)
{
int num,flag=0,newNumber;
cout<<"Enter Number You Want To Replace:";
cin>>num;
for(int i=0;i<n;i++)
{
if(arr[i]==num)
{
flag=1;
}
}
if(flag==1)
{
cout<<"Enter New Number:";
cin>>newNumber;
for(int i=0;i<n;i++)
{
if(arr[i]==num)
{
arr[i]=newNumber;
}
}
cout<<"Number Replaced Successfully!!!!"<<endl;
cout<<"Array After Replacment of the Number: ";
DisplayAllNumbers(arr,n);
}
else if(flag==0)
{
cout<<"Number Not Found."<<endl;
}
}
void AverageOfNumbers(int arr[],int n)
{
float avg,sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
avg=sum/n;
cout<<"Average of all Numbers:"<<avg<<endl;
}
void LargestNumber(int arr[],int n)
{
int max=arr[0];
for(int i=1;i<n;i++)
{
if(max<arr[i])
{
max=arr[i];
}
}
cout<<"Largest Number in Array:"<<max<<endl;
}
void SumOfOdds(int arr[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
sum=sum+arr[i];
}
}
cout<<"Sum of all Odd Numbers:"<<sum<<endl;
}
int main()
{
int arr[10],n,opt;
cout<<"First Create Array!!!!!!"<<endl;
cout<<"Total No you Want to Enter:";
cin>>n;
cout<<"Enter Numbers:"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Array Create Successfully!!!!"<<endl;
do
{
opt=interface();
if(opt==1)
{
AddNewNumber(arr,n);
}
else if(opt==2)
{
DisplayAllNumbers(arr,n);

}
else if(opt==3)
{
FrequencyOfNumber(arr,n);
}
else if(opt==4)
{
FindAndReplace(arr,n);
}
else if(opt==5)
{
AverageOfNumbers(arr,n);
}
else if(opt==6)
{
LargestNumber(arr,n);
}
else if(opt==7)
{
SumOfOdds(arr,n);
}
else if(opt==8)
{
cout<<"Program End!!!!!!!!!!!!!";
}
else
{
cout<<"Plz enter correct opition.";
}
}while(opt!=8);
}

RUN:

Initilize Array And Display Interface


Add New Number

Display All Numbers


Frequency of Number

Find and Replacement of Number

If not found:
Average

Largest Number
Sum Of Odds

Exit

You might also like