can someone help me create a header file and main function, question asked for i
ID: 656189 • Letter: C
Question
can someone help me create a header file and main function,
question asked for integers from the keyboard in fixed-size array,array in reverse order,searches the largest number in the array and smallest.
code i got
List::List(){
for(int i=0; i<100;i++){
number[i]=0.0;
}
}
void List::input(){
string keyboard;
double digit;
int index=0;
cout<<"Enter number ";
cout<<"Enter number with a space ";
getline(cin,keyboard);
istringstream iss(keyboard);
while(iss.good()){
iss>> digit;
number[index]=digit;
index++;
}
endPoint=index;
cout<
//display back
for(int i=0;i
cout<
//reverse order
void List::reverse(int curIndex){
if(curIndex>=0){
cout<< number[curIndex]<<" ";
reverse(curIndex-1);
}
}
//largest number
void List::larNumber(int numList[],int first,int last){
if (first == last) {
cout<<"The largest number's index: "<< first< cout<<"The largest number: "<< numList[first]<
} else if (numList[first] < numList[last]) {
if(first< last){
largestNumber(numList, first + 1, last);
}else if (first>last){
largestNumber(numList, first - 1, last);
}
} else if (numList[first] > numList[last]){
int temp= first;
first= last;
last= temp;
largestNumber(numList, first, last);
}
}
//smallest number
int List::kthSmallest(int numList[], int left,int right, int k){
int i = left, j = right;
int tmp;
int pivot = numList[(left + right) / 2];
while (i <= j) {
while (numList[i] > pivot)
i++;
while (numList[j] < pivot)
j--;
if (i <= j) {
tmp = numList[i];
numList[i] = numList[j];
numList[j] = tmp;
i++;
j--;
}
};
if (left < j)
kthSmallest(numList, left, j,k);
if (i < right)
kthSmallest(numList, i, right,k);
return numList[endPoint-k];
}
Explanation / Answer
//List header file List.h
class List
{
int number[100]; //2 data members
int endPoint;
public:
List(); //Constructor
void input(); //methods signatures only implementation is in main program
void reverse(int curIndex);
void largestNumber(int numList[],int first,int last);
int kthSmallest(int numList[], int left,int right, int k);
};
//mainfunct.cpp
#include <iostream>
#include "List.h"
using namespace std;
List::List(){ //Defining constructor
for(int i=0; i<100;i++){
number[i]=0.0; //Initialization
endPoint=0;
}
}
void List::input(){ //Input function
string keyboard; //keyboard variables
double digit;
int index=0;
cout<<"Enter number ";
cout<<"Enter number with a space ";
getline(cin,keyboard); //Reading each line
istringstream iss(keyboard);
while(iss.good()){ //reading each number into array
iss>> digit;
number[index]=digit;
index++;
}
endPoint=index;
cout<<endPoint;
//display back
for(int i=0;i<endPoint;i++)
cout<<" "<<number[i];
}
//reverse order
void List::reverse(int curIndex){
if(curIndex>=0){
cout<< number[curIndex]<<" ";
reverse(curIndex-1);
}
}
//largest number
void List::largestNumber(int numList[],int first,int last){
if (first == last) {
cout<<"The largest number's index: "<< first;
cout<<"The largest number: "<< numList[first];
} else if (numList[first] < numList[last]) {
if(first< last){
largestNumber(numList, first + 1, last);
}else if (first>last){
largestNumber(numList, first - 1, last);
}
} else if (numList[first] > numList[last]){
int temp= first;
first= last;
last= temp;
largestNumber(numList, first, last);
}
}
//smallest number
int List::kthSmallest(int numList[], int left,int right, int k){
int i = left, j = right;
int tmp;
int pivot = numList[(left + right) / 2];
while (i <= j) {
while (numList[i] > pivot)
i++;
while (numList[j] < pivot)
j--;
if (i <= j) {
tmp = numList[i];
numList[i] = numList[j];
numList[j] = tmp;
i++;
j--;
}
};
if (left < j)
kthSmallest(numList, left, j,k);
if (i < right)
kthSmallest(numList, i, right,k);
return numList[endPoint-k];
}
//Here is my code
int main()
{
int A[]={78,3,13,32,1,88,2};
List l1();
l1.input();
l1.reverse(5);
l1.larNumber(A,0,6);
cout<<" 2nd smallest in array : "<<l1.kthSmallest(A,0,6,2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.