In: Computer Science
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;
}
}
}
}
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 :