Using C++ (use prototype function) Create a program with one main function and a
ID: 3718499 • Letter: U
Question
Using C++ (use prototype function)
Create a program with one main function and at least four other functions.
Create a string called name in the main function to hold a complete first and last name. Call a function to ask the user to enter the first name. Then ask the user to enter the second name. Concatenate the two names together so it will print out as a first name space last name with the other callouts at the end. Return the concatenated name as the return value of the function to main.
Create an array called addIt that can contain 10 integers in the main function. Call a function, passing the array addIt that will allow the user to enter up to 10 integers. If the user enters -99 then the loop should stop there and return to the main function, otherwise, accept 10 integers before returning.
From main, pass the array addIt to a function that will loop to step through all the numbers in the array and add them together. Return the sum of all the numbers as the return value of the function to main.
From main, pass the array to a function that will sort the array in a descending order.
Print to the screen the full name entered. Then print the currently sorted array and the sum of all the numbers in the array. Use a format that tells me what each item is, ie “The name is: “ “The sum of the array is: “ “The array is as follows, sorted descending: “
Thoughtfully comment your code. And use prototype function!
Explanation / Answer
Please find my implementation.
#include<iostream>
#include<string>
using namespace std;
// Declare prototype functions
string cat(string fName,string lName);
int readArray(int a[]);
int sumOfArray(int addIt[],int size);
void sortDesc(int a[],int size);
void display(int a[],int size);
int main()
{
string s1,s2,s3; // declare string objects
cout<<"Enter First Name: ";
cin>>s1; // read first string
cout<<" Enter Second Name: ";
cin>>s2; // read second string
s3=cat(s1,s2); // calling cat() function and receive returning elements into s3
cout<<" The Name is: "<<s3; // display returned element s3
int a[10]; // declare an array of integer elements size is 10
int size=readArray(a); // calling readArray() function for read an array elements and returned total number of an array elements into size
int s=sumOfArray(a,size); // calling sumOfArray() function and returned value into s
cout<<" The sum of the array is: "<<s<<endl; // display sum of an array elements
cout<<"The array is as follows, sorted descending: ";
sortDesc(a,size); // calling sortDesc() function for sorting an array elements into descending order
display(a,size); // calling display() function for display sorted elements
return 0;
}
// implement cat() function and return string
string cat(string fName,string lName)
{
return fName+" "+lName; // return joining fName and lName with space;
}
// implement readArray() function and return number of elements in the array
int readArray(int a[])
{
int n,count=0; // declare integer variables
for(int i=0;i<10;i++) // create for loop until 10 elements
{
cout<<" Enter Integer Number: "; cin>>n; // read integer elements
if(n<0) break; // if n is negative integer then break from loop
else
a[i]=n; // assign each number to an array list
count++; // couting number of elements
}
return count; // return number of elements in the array
}
// implement sumOfArray() function and return sum
int sumOfArray(int addIt[],int size)
{
int sum=0,n; // declare integer variables
for(int i=0;i<size;i++) // create for loop until size of an array elements
{
sum+=addIt[i]; // calculate sum of an array elements
}
return sum; // return sum of an array elements
}
// implement sortDesc() function using bubble sort
void sortDesc(int a[],int size)
{
for(int i=0;i<size;i++) // create outer for loop until size of an array elements
for(int j=0;j<(size-i);j++) // create inner for loop until size-i of an array elements
if(a[j]<a[j+1]) // check condition smallest element < biggest element then
{
// swapping of an array elements
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
// implement display() function for an array elements
void display(int a[],int size)
{
for(int i=0;i<size;i++) // create for loop unti size of an array
cout<<a[i]<<" "; // display an array elements
}
OUTPUT-1
Enter First Name: XXXX
Enter Second Name: YYYY
The Name is: XXXX YYYY
Enter Integer Number: 1
Enter Integer Number: 2
Enter Integer Number: 3
Enter Integer Number: 4
Enter Integer Number: 5
Enter Integer Number: 6
Enter Integer Number: 7
Enter Integer Number: 8
Enter Integer Number: 9
Enter Integer Number: 10
The sum of the array is: 55
The array is as follows, sorted descending: 10 9 8 7 6 5 4 3 2 1
OUTPUT-2
Enter First Name: XXXX
Enter Second Name: YYYY
The Name is: XXXX YYYY
Enter Integer Number: 55
Enter Integer Number: 4
Enter Integer Number: 33
Enter Integer Number: -99
The sum of the array is: 92
The array is as follows, sorted descending: 55 33 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.