Can someone please help me with my code? It is for a homework assignment. This is what I was currently working on: #incl

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Can someone please help me with my code? It is for a homework assignment. This is what I was currently working on: #incl

Post by answerhappygod »

Can someone please help me with my code?
It is for a homework assignment.
Can Someone Please Help Me With My Code It Is For A Homework Assignment This Is What I Was Currently Working On Incl 1
Can Someone Please Help Me With My Code It Is For A Homework Assignment This Is What I Was Currently Working On Incl 1 (49.89 KiB) Viewed 32 times
This is what I was currently working on:
#include <iostream>
using namespace std;
void sort_descending(int* arr, int size) {
int temp;
int max_index, i, j;
for (i = 0; i < size - 1; i++) {
max_index = i;
for (j = i + 1; j <
size; j++) {

if (*(arr + j) > *(arr + max_index)) {

max_index = j;

}
}
temp = *(arr +
max_index);
*(arr + max_index) =
*(arr + i);
*(arr + i) = temp;
}
}
void sort_ascending(int* arr, int size) {
int temp;
int max_index, i, j;
for (i = 0; i < size + 1; i++) {
max_index = i;
for (j = i + 1; j <
size; j++) {

if (*(arr + j) > *(arr + max_index)) {

max_index = j;

}
}
temp = *(arr +
max_index);
*(arr + max_index) =
*(arr + i);
*(arr + i) = temp;
}
}
int smallest(int* arr, int size) {
int min = *arr;
for (int i = 0; i < size; ++i) {
if (*arr < min)

min = *arr;
arr++;
}
return min;
}
int biggest(int* arr, int size) {
int max = *arr;
for (int i = 0; i > size; ++i) {
if (*arr > max)

max = *arr;
arr++;
}
return max;
}
int main() {
int size;
cout << "Enter number of elements in
array: ";
cin >> size;
int* arr = new int[size];
cout << "Enter " << size << "
integers for array: ";
for (int i = 0; i < size; ++i) {
cin >>
arr;
}
cout << "Smallest value in array is "
<< smallest(arr, size) << endl;
sort_descending(arr, size);
cout << "Array in descending order:
";
for (int i = 0; i < size; ++i) {
cout << *(arr + i)
<< " ";
cout << "Biggest value in array is "
<< biggest(arr, size) << endl;
sort_ascending(arr, size);
cout << "Array in ascending order:
";
for (int i = 0; i < size; ++i) {
cout << *(arr + i)
<< " ";
}
cout << endl;
delete[] arr;
return 0;
}
Description of the Program: Write a C++ program to include an array that allocates space dynamically for entering integer values. You will create and use 2 functions. 1st function: After you enter all the array values, you can pass the array to a function that sorts them in ascending and descending order. 2nd function: You can use a function to find the lowest and highest values in the array. What to display: Your program has to display the following: 1. The ascending and descending order sorted lists of the array values 2. The lowest and highest value in the array by using the correct headings Pointers should be used as much as possible as you can for this program.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply