Question

Task 1: Hardcode the algorithm == Write the loops and if statements necessary for an algorithm....

Task 1:
Hardcode the algorithm == Write the loops and if statements necessary for an algorithm. Do not use library calls.

Main Function: Declare and fill an array with 1000 random integers between 1 - 1000.
You will pass the array into functions that will perform operations on the array.
Function 1:
Create a function that will output all the integers in the array. (Make sure output is clearly formatted.
Function2:
Create a Function that will sum and output all the odd numbers in the array.
Function 3:
Create a Function that will sum and output all the even numbers in the array.
Function 4:
Create a function that will allow the user to enter an integer value to search. Program will output if integer value is found and the location of the integer in the array. or The program will output “value is not in array”. (“value is not in array should not be printed more than once.”)
Function 5:
Create a function that will output the highest value in the array.
(Hardcode the algorithm.)
Function 6:
Create a function that will output the lowest value in the array.
(Hardcode the algorithm.)
Function 7:
Sort and then output values. Hardcode the bubble sort algorithm you will implement.
Function 8:
Create a function that will output the first value in the array.
Function 9:
Create a function that will output the last value in the array.
Function 10:
Create a function that will output the middle value in the array.
Function 11:
Create a function that will output the sum of all value in the array.

Make sure you have a menu operating the program until user decides to exit.
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: bubble sort
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: Output middle Number in the Array
Enter 11: Output the sum of all value in the Array
Enter 12: to Exit Program

C++ Please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
void output(int a[], int s);
void oddsum(int a[], int s);
void evensum(int a[], int s);
void search(int a[], int s);
void highest(int a[],int s);
void lowest(int a[], int s);
void bubble(int a[], int s);
void firstele(int a[]);
void lastele(int a[], int s);
void middle(int a[],int s);
void sum(int a[],int s);
int main() {
int a[1000],n,choice,i;
/*a is the array woth 1000 memory location, n for number of elemebts and choicr for choice entering*/
cout<<"Ente Array ";
cin>>n;
cout<<"Enter the numbers:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
/*for reading array elements in each of the i'th position.*/
do {
cout<<"\n\nMENU";
cout<<"\n1. Output all Integer values";
cout<<"\n2. Sum of all Odd numbers:"; cout<<"\n3. Sum of all even numbers:";
cout<<"\n4. Enter a search value:";
cout<<"\n5. Output the highest number:";
cout<<"\n6. Output the lowest number:";

cout<<"\n7. Bubble Sort:";
cout<<"\n8. Output the first element in the Array:";
cout<<"\n9. Output the last element in the Array:";
cout<<"\n10. Output the Middle number in the Array";
cout<<"\n11. Output the sum of all numbers in the Array:";
cout<<"\n12. Exit";
//these are menu items

cout<<"\n\nEnter your choice 1-12:"; cin>>choice;
//reading choice from 1-12
switch(choice)
{
case 1:
output(a,n);
/*if 1 is entered then call output method*/

break;
case 2:oddsum(a,n); break;
/*choice entered is 2 means calling oddsum()*/
case 3: evensum(a,n); break;
/*3 for calling evensum()*/
case 4: search(a,n); break;
/* 4 option is for calling search a number method*/
case 5: highest(a,n); break;
/*5 for highest()*/
case 6:
lowest(a,n);
/*calling lowest()*/
break;
case 7:bubble(a,n); break;
/*bubblesort*/
case 8: firstele(a); break;

case 9: lastele(a,n); break;
case 10: middle(a,n); break;
case 11: sum(a,n);break;
case 12:break;
default:cout<<"\nInvalid choice";
}
}while(choice!=12);
/*if choice is equal to 12, then exit from the switch*/
return 0;
}
void output(int a[], int s)
{
cout<<"Entered elements are:";
for(int i=0;i<s;i++)
{
cout<<a[i];
}
/*printing elements from each podition of the i'th element in array a*/
}
   void oddsum(int a[], int s)
    {
    int sum=0;
    cout<<"The sum of all odd numbers in the array are:\n";
   for(int i=0;i<s;i++)
   {
   if(a[i]%2!=0)
   {
   sum=sum+a[i];
   }
   /*a[i]%2 gives the reminder if it is not equal to zero, then odd num add to sum*/
   }
   cout<<"Sum of odd numbers="<<sum;
   }
   void search(int a[],int s)
   {
   int num,count=0;
   cout<<"enter the element that you want to search:";
cin>>num;
for(int i=0;i<s;i++)
{
if(a[i]==num)
{
count=1;
cout<<"element present in the array with position"<<i+1;
break;
}
}
/* this function search an element, if a[i] is equal to num then present int the Arrray"*/
if (count==0)
cout<<"element not present in the array";
}
  
   void evensum(int a[], int s)
    {
    int sum=0;
    cout<<"The sum of all odd numbers in the array are:\n";
   for(int i=0;i<s;i++)
   {
   if(a[i]%2==0)
   {
   sum=sum+a[i];
   }
  
   }
   cout<<"Sum of even numbers="<<sum;
   }
  
void highest(int a[], int s)
    {
    int max;
   
   for(int i=0;i<s;i++)
   {
   for(int j=i+1;j<s;j++)
   {
   if(a[i]>a[j])
   {
   max=a[i];
   }
   }
   /*this function compares each element, with other then find highest value*/
   }
   cout<<"Highest number=" << max;
   }
  
void lowest(int a[], int s)
    {
   
    int min=a[0];
   for(int i=0;i<s;i++)
   {
   for(int j=i+1;j<s;j++)
   {
   if(a[i]<a[j])
   {
   min=a[i];
   }
   }
   /* compares and find lowest value*/
   }
   cout<<"lowest number="<<min;
   }   
   void bubble(int a[],int s)
   {
   int i,j,temp;
   for(i=0;i<s-1;i++)
   {
   for(j=0;j<(s-1-i);j++)  
   {         
   if(a[j]>a[j+1])
   {
   temp=a[j]; //swapping
   a[j]=a[j+1];
   a[j+1]=temp;
   }
   }
   }
}

void firstele(int a[])
    {
   
    cout<<"The first element in the array is: "<<a[0];
   //first element is in a[0]th position
   }
  
  
void lastele(int a[],int n)
    {
   
    cout<<"The last element in the array is: "<<a[n-1];
   //last element is in n-1th position
   }
  
   
void middle(int a[],int n)
    {
    int m=n/2;
    cout<<"The middle element in the array is: "<<a[m];
   //m contains the minddle value
   }
  
   void sum(int a[],int n)
    {
    int s=0;
    for(int i=0;i<n;i++)
    {
    s=s+a[i];
    }
    cout<<"The sum of the array is: "<<s;
    /*s calculates the sum of each a[i] value*/
  
   }

Add a comment
Know the answer?
Add Answer to:
Task 1: Hardcode the algorithm == Write the loops and if statements necessary for an algorithm....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT