Question

In: Computer Science

Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a...

Language: C++

NEEDS TO WORK IN VISUAL BASIC

The code is broken and loops in a few places please fix it

#include<iostream>
using namespace std;
//function declaration
void EnterRents(int*, int);
void displayRents(int*, int);
void selectionSort(int*, int);
int sumRents(int* temp, int size)
{
   int sum = 0;
   for (int i = 0; i < size; i++)
   {
       sum += *(temp + i);
   }
   return sum;
}
void Displaymemory(int* temp, int size)
{
   /*int memory;
   memory=sizeof(temp)*size;
   cout<<memory;*/
   for (int i = 0; i < size; i++)
   {
       cout << &temp[i] << " ";
   }
}
//main drive
int main()
{
   int n;

   char c;

   cout << "Enter a for enter the rents amounts" << endl;
   cout << "Enter b to display rents amounts" << endl;
   cout << "Enter c to sort the rents amounts" << endl;
   cout << "Enter d to total rents amounts" << endl;
   cout << "Enter e to display memory alloaction" << endl;
   cout << "Enter f to exit" << endl;
   cout << "Enter the number of amount items stored in Amout array " << endl;
   cin >> n;
   // we should create the array with help of new if we are giving size dynamically
   int* arr = new int[n];
   //while statement with switch case to call declared function
   while (1)
   {
       cout << endl;
       cout << "Enter your choice" << endl;
       cin >> c;
       switch (c)
       {
       case 'a':
           cout << "Enter the " << n << " no of Rents amount" << endl;
           EnterRents(arr, n);
           break;
           //Other switch cases
       case 'b':
           cout << "Display Rents" << endl;
           displayRents(arr, n);
           break;
       case 'c':
           cout << "sort Rents amounts" << endl;
           selectionSort(arr, n);
           cout << "sorted elemnts are" << endl;
           displayRents(arr, n);
           break;
       case 'd':
           cout << "Total Rents" << endl;
           cout << sumRents(arr, n) << endl;
           break;
       case 'e':
           cout << "Display memoryLocation" << endl;
           Displaymemory(arr, n);
           break;
       case 'f':
           cout << "f entered by user to exit" << endl;
           exit(1);
       default:
           cout << "Invalid input.";
           break;

       }
   }

   return 0;
}
//function definition
void EnterRents(int* arr, int size)
{
   for (int i = 0; i < size; i++)
   {
       cin >> arr[i];
   }
}
void displayRents(int* arr, int n)
{
   for (int i = 0; i < n; i++)
   {
       cout << *(arr + i) << " ";
   }
}
//void selectionSort(int*,int)
void selectionSort(int* pointer, int size)
{
   int* i, * j, swap;
   int* end = NULL;

   if (size < 2 || pointer == NULL)
       return;

   end = pointer + size - 1;

   for (i = pointer; i < end; i++)
   {
       for (j = i + 1; j <= end; j++)
       {
           if (*j < *i)
           {
               swap = *i;
               *i = *j;
               *j = swap;
           }
       }
   }
}

Solutions

Expert Solution

Solution :

Following is the corrected code :

#include<iostream>
using namespace std;
//function declaration
void EnterRents(int*, int);
void displayRents(int*, int);
void selectionSort(int*, int);
int sumRents(int* temp, int size)
{
   int sum = 0;
   for (int i = 0; i < size; i++)
   {
       sum += *(temp + i);
   }
   return sum;
}
void Displaymemory(int* temp, int size)
{
   /*int memory;
   memory=sizeof(temp)*size;
   cout<<memory;*/
   for (int i = 0; i < size; i++)
   {
       cout << &temp[i] << " ";
   }
}
//main drive
int main()
{  int n;
   char c;
   cout << "Enter a for enter the rents amounts" << endl;
   cout << "Enter b to display rents amounts" << endl;
   cout << "Enter c to sort the rents amounts" << endl;
   cout << "Enter d to total rents amounts" << endl;
   cout << "Enter e to display memory alloaction" << endl;
   cout << "Enter f to exit" << endl;
   cout << "Enter the number of amount items stored in Amout array " << endl;
   cin >> n;
   // we should create the array with help of new if we are giving size dynamically
   int* arr = new int[n];
   //while statement with switch case to call declared function
   while (1)
   {
       cout << endl;
       cout << "Enter your choice" << endl;
       cin >> c;
       switch (c)
       {
       case 'a':
           cout << "Enter the " << n << " no of Rents amount" << endl;
           EnterRents(arr, n);
           break;
           //Other switch cases
       case 'b':
           cout << "Display Rents" << endl;
           displayRents(arr, n);
           break;
       case 'c':
           cout << "sort Rents amounts" << endl;
           selectionSort(arr, n);
           cout << "sorted elemnts are" << endl;
           displayRents(arr, n);
           break;
       case 'd':
           cout << "Total Rents" << endl;
           cout << sumRents(arr, n) << endl;
           break;
       case 'e':
           cout << "Display memoryLocation" << endl;
           Displaymemory(arr, n);
           break;
       case 'f':
           cout << "f entered by user to exit" << endl;
           exit(1);
       default:
           cout << "Invalid input.";
           break;

       }
   }

   return 0;
}
//function definition
void EnterRents(int* arr, int size)
{
   for (int i = 0; i < size; i++)
   {
       cin >> arr[i];
   }
}
void displayRents(int* arr, int n)
{
   for (int i = 0; i < n; i++)
   {
       cout << *(arr + i) << " ";
   }
}
//void selectionSort(int*,int)
void selectionSort(int* pointer, int size)
{
   int* i, * j, swap;
   int* end = NULL;

   if (size < 2 || pointer == NULL)
       return;

   end = pointer + size - 1;

   for (i = pointer; i < end; i++)
   {
       for (j = i + 1; j <= end; j++)
       {
           if (*j < *i)
           {
               swap = *i;
               *i = *j;
               *j = swap;
           }
       }
   }
}

Code demo :


Related Solutions

Language: c++ works in visual basic Write a program that uses an array of nested structs...
Language: c++ works in visual basic Write a program that uses an array of nested structs to store the addresses for your store’s customers.  Each customer has a name and two addresses: home address and business address.  Each address has a street, city, state, and zip code. Requirements: 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language:C++ NEEDS TO WORK IN VISUAL BASIC error on line 41 expression must have a constant...
Language:C++ NEEDS TO WORK IN VISUAL BASIC error on line 41 expression must have a constant value #include using namespace std; //function declaration void EnterRents(int*, int); void displayRents(int*, int); void selectionSort(int*, int); int sumRents(int* temp, int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += *(temp + i); } return sum; } void Displaymemory(int* temp, int size) { /*int memory; memory=sizeof(temp)*size; cout< for (int i = 0; i < size; i++)...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
Make a Console application Language should be Visual Basic In this assignment, you will be calculating...
Make a Console application Language should be Visual Basic In this assignment, you will be calculating the two parts for each month. you calculate the interest to pay each month and principal you pay every month. The interest you pay every month = loan * monthlyInterest The principal you pay every month = monthlyMortgage -  interest you pay every month ' what is my remaining loan loan = loan - principal you pay every month Problem 1 : Using While Loop,...
Make a Console application Language should be Visual Basic You will be simulating an ATM machine...
Make a Console application Language should be Visual Basic You will be simulating an ATM machine as much as possible Pretend you have an initial deposit of 1000.00. You will Prompt the user with a Main menu: Enter 1 to deposit Enter 2 to Withdraw Enter 3 to Print Balance Enter 4 to quit When the user enters 1 in the main menu, your program will prompt the user to enter the deposit amount. If the user enter more than...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some time and still can't quite figure it out. I'm creating an app that has 2 textboxes, 1 for inputting customer name, and the second for entering the number of tickets the customer wants to purchase. There are 3 listboxes, the first with the days of the week, the second with 4 different theaters, and the third listbox is to display the customer name, number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT