Given an array, A, that contains datasize number of values: Write C++ code segme
ID: 3680298 • Letter: G
Question
Given an array, A, that contains datasize number of values: Write C++ code segments to do each of the following:
1. Search the array for a particular value. Assign a boolean value with true if the search value is found in the array, and false if the value is not found. Write out a ‘found’ message if the boolean value is true and a ‘not found’ message if not.
2. Search the array for a particular value. Assign an integer with the value of -1 if the search value is not found in the array, and the array subscript where it was found if the value is present in the array. Write out the search value and the location in which it was found if it was found, and a ‘not found’ message if not.
3. Open an input file named “input.txt”. Ask the user for the name of an output file and open that file for output. Read in 3 values from the input file and write them out to the output file.
4. Declare a struct type named Employee that contains: name: a string hourlyWage: a double hoursPerDay: an array of 7 integers Give main program statements that would declare one struct variable and read values into all elements from the keyboard.
Explanation / Answer
1)/**C++ code to search an array of elements with size.
Then print a message if the element is found*/
#include<iostream>
using namespace std;
int main()
{
//Create a constant size
const int size=10;
//Create an array
int arr[size]={5,6,7,9,10,12,14,15,16,17};
//Set found to false
bool found=false;
//set search value
int search=17;
//run through the array and check if search element is found
for(int index=0;
index<size && !found;index++)
{
if(arr[index]==search)
found=true;
}
//Check if found is true
if(found)
//print elmenet is found
cout<<"Element is found"<<endl;
else
//otherwise print element is not found
cout<<"Element is not found"<<endl;
system("pause");
return 0;
}
Sample output:
Element is found
----------------------------------------------------------------------------------
2)
/**C++ code to search an array of elements with size.
Then print a position of an element if the element is found*/
#include<iostream>
using namespace std;
int main()
{
//Create a constant size
const int size=10;
//Create an array
int arr[size]={5,6,7,9,10,12,14,15,16,17};
//Set pos to -1
int pos=-1;
//set search value
int search=17;
//run through the array and check if search element is found
for(int index=0;
index<size;index++)
{
if(arr[index]==search)
{
//set index to pos
pos=index;
}
}
//Check if pos is not -1
if(pos!=-1)
//print elmenet is found
cout<<"Element is found at"<<pos<<endl;
else
//otherwise print element is not found
cout<<"Element is not found"<<endl;
system("pause");
return 0;
}
Sample output:
Element is found at 9
---------------------------------------------------------------------------------------------------
3)
/**C++ program reads output file name from user and writes
three numbers from input.txt file to the user entered file name*/
//headaer files
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a,b,c;
char input[40]="input.txt";
char output[40];
//Create ifstream object
ifstream fin;
fin.open(input);
//Check if input file exists or not
if(!fin)
{
cout<<"File doesnot exist"<<endl;
exit(0);
}
cout<<"Enter output file";
cin>>output;
//Create an output file object and open a text file
ofstream fout;
fout.open(output);
//read from input file
fin>>a>>b>>c;
//write to output file
fout<<a<<endl;
fout<<b<<endl;
fout<<c<<endl;
//close the input file
fin.close();
//close the output file
fout.close();
//pause the program output
system("pause");
return 0;
}
Sample output:
input.txt
1
2
3
Enter output file output.txt
output.txt
1
2
3
--------------------------------------------------------------------------------
4)
/**C++ program that prompts user to enter name, hourly wage and number of hours
worked for 7 days and print the results to console */
#include<iostream>
#include<string>
using namespace std;
//Employee structure array
struct Employee
{
string name;
double hourlyWage;
int hoursPerDay[7];
};
int main()
{
//Employee variable
Employee emp;
cout<<"Enter name of the employee :";
cin>>emp.name;
cout<<"Enter hourly wage of the employee :";
cin>>emp.hourlyWage;
for(int day=0;day<7;day++)
{
cout<<"Enter day "<<day+1<<" hours: ";
cin>>emp.hoursPerDay[day];
}
cout<<"Name : "<<emp.name<<endl;
cout<<"Hourly Wage : "<<emp.hourlyWage<<endl;
cout<<"Hours per day : "<<endl;
for(int day=0;day<7;day++)
{
cout<<"Day : "<<(day+1)<<" Hours : "<<emp.hoursPerDay[day]<<endl;
}
system("pause");
return 0;
}
Sample output:
Enter name of the employee :johnson
Enter hourly wage of the employee :20
Enter day 1 hours: 40
Enter day 2 hours: 35
Enter day 3 hours: 35
Enter day 4 hours: 40
Enter day 5 hours: 35
Enter day 6 hours: 35
Enter day 7 hours: 35
Name : johnson
Hourly Wage : 20
Hours per day :
Day : 1 Hours : 40
Day : 2 Hours : 35
Day : 3 Hours : 35
Day : 4 Hours : 40
Day : 5 Hours : 35
Day : 6 Hours : 35
Day : 7 Hours : 35
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.