Question

In: Computer Science

#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...

#include <string>

using namespace std;

//using recursion no loops allowed


int main()

{

double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };

  //Challenge #2: print the list from given range

  printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6

  cout << endl;

  //Challenge #3: print the list, but backwards

  printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8

  cout << endl;

              

  //Challenge #4: reverse order of items in list

  reverse(nums, 0, 12);

  printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8

}

c++

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

//using recursion no loops allowed

// this function prints the elements of array in forward direction
void printList(double nums[],int st,int en)
{
    for(int i=st;i<=en;i++)
        cout<<nums[i]<<" ";
    cout<<"\n";
}

// this function prints the elements of array in reverse direction
void printReverse(double nums[],int st,int en)
{
    for(int i=en;i>=st;i--)
        cout<<nums[i]<<" ";
    cout<<"\n";
}

// this function reverses the array
void reverse(double nums[],int st,int en)
{
    // we swap all the elements from begin to mid position with the elements at the same position from the end
    int mid=(st+en)/2;
    for(int i=st;i<=mid;i++)
    {
        swap(nums[i],nums[en-i]);
    }
}

int main()

{

  double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };

  //Challenge #2: print the list from given range

  printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6

  cout << endl;

  //Challenge #3: print the list, but backwards

  printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8

  cout << endl;



  //Challenge #4: reverse order of items in list

  reverse(nums, 0, 12);

  printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8

}

PLEASE LIKE THE SOLUTION :))

IF YOU HAVE ANY DOUBTS PLEASE MENTION IN THE COMMENT


Related Solutions

#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT