I need to calculate the sum of negative elements of an array located between the largest and the smallest elements. Here's the code I've got so far:
#include <iostream>
using namespace std;
int main() {
int i = 0;
int arrSize = 0;
cout << "Enter an array size" << endl;
cin >> arrSize;
if (arrSize <= 0) {
cerr << "Entered number is negative or 0" << endl;
return 0;
} // check if arrSize is 0 or negative, print error if so
double* arr = new double [arrSize]; //declare array
for (i = 0; i < arrSize; i++) {
cout << "Enter array element " << i+1 << endl;
cin >> arr[i];
} // get elements of array
int max = arr[0];
for (int i = 1; i < arrSize; i++) {
if (max < arr[i])
max = arr[i];
} //find max value of array
int min = arr[0];
for (int i = 1; i < arrSize; i++) {
if (min > arr[i])
min = arr[i];
} //find min value of array
How would I go about finding the positions of max and min elements and getting the range in between them?
Related
Why is the output of this C++ code to find the min and max between inputted elements not showing the lowest number but rather the 2nd to the lowest?
Idk why the min is not working when it is the exact opposite code of the max which is perfectly showing the highest number.
#include <iostream>
using namespace std;
int main()
{
int array[50], *max, *min, size, i;
cout<<"Enter the number of elements in array\n";
cin>>size;
cout<<"Enter array elements\n";
for ( i = 0 ; i < size ; i++ )
cin>>array[i];
max = array;
min = array;
for (i = 0; i < size; i++)
{
if (*(array+i) > *max)
*max = *(array+i);
}
cout<<"Maximum element in the array is "<< *max << "\n" ;
for (i = 0; i < size; i++)
{
if (*(array+i) < *min)
*min = *(array+i);
}
cout<<"Minimum element in the array is "<< *min <<"\n";
return 0;
}
the Question was
Q.You are given an array of n elements. Each element depicts the height of a vertical bar.
For example, if you take the input array as [6,2,1,3] then the output should be.
what i though was
take value from user as a array
find the max value in that array
create a 2d boolean array with height as max value and width as array size of the user input array.
fill the boolean array vertically instead of horizontaly (methord how i did it is in the code below).
print the boolean array.
CODE:
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "enter the size of array: ";
cin >> n;
int arr[n];
//INPUTING IN ARRAY!
for (i = 0; i < n; i++) {
cin >> arr[i];
}
//Calculating max-element
int max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
//creating a boolean array with height - max , width - n
bool new_arr[max][n];
//varables to input in the array vertically.
int k = 0, Q = 0;
for (i = 0; i < n; i++) {
for (j = max; j > 0; j--) {
Q = max - j; //to input in the array vertically, i.e at i =0 ,Q =0,1,2,3,4,5.
if (arr[i] >= j) {
new_arr[k][Q] = 1;
}
else {
new_arr[k][Q] = 0;
}
}
k++;
}
//printing the boolean array
cout << "FINAL ARRAY :" << endl;
for (i = 0; i < max; i++) {
for (j = 0; j < n; j++) {
cout << new_arr[i][j] << "\t";
}
cout << endl;
}
return 0;
}
I know there other far better methods but my mind is taken aback by this thought of doing
this question by this way only, I have tried another way and was successful but I am currently failing to do this question by this method(which my mind though of), please help.
my program is focusing on Array elements for this lab, but I am not sure on how to set my average to a specific number that is requested. Any guidance would be helpful for this post
#include "pch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int arr[10], n, i, max, min, avg;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
avg = arr[0];
for (i = 0; i < n; i++)
{
if (avg > arr[i])
avg = arr[i];
}
cout << "Largest element: " << max;
cout << "Smallest element: " << min;
cout << "Average element: " << avg;
}
I would recommend using the += operator in your for loop and then dividing by n.
float sum = 0;
float avg = 0;
for(i = 0; i < n; i++)
{
sum += arr[i];
}
avg = sum / n;
Also, I would recommend using either a float or double instead of using an int for your average because otherwise you're doing integer division which will cut off the decimal. i.e. 5 / 2 = 2
If I enter an array , at first the code finds the minimums then I want to put zeroes after all the minimums . For example
given an array = 1,1,3,1,1
As we see 1s are the minimum so the result should be = 1,0,1,0,3,1,0,1,0
CODE
#include <pch.h>
#include <iostream>
int main()
{
int min = 10000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] > min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the
i++; // `for loop k` to be precise)
n++;
}
std::cout << array[i] << ", 0";
}
return 0;
}
But my answer doen't put zeroes exactly after minimums
There are few issues in your code, first of all your min is wrong. I have fixed your code with comments on fixes I have made. Please take a look :
#include "stdafx.h"
#include <iostream>
int main()
{
int min = 10000;
bool found = 0;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min) //< instead of >
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min)
{
for (int k = n; k > i; k--)
{
array[k] = array[k - 1];
}
array[i + 1] = 0;
i++; //increment i here because you don't want to consider 0 that you have just added above.
n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
}
}
//print the array separately
for (int i = 0; i < n; i++)
{
std::cout << array[i];
if (i != n - 1)
{
std::cout << ",";
}
}
return 0;
}
The first issue was in the calculation of min: < instead of >.
Another problem if that you are modifyng the paramers iand ninside the loop. This is rather dangerous and implies to be very cautious.
Another issue was that it should be i++; n++; instead of i--,n--;
Here is the code:
// #include <pch.h>
#include <iostream>
int main()
{
int min = 1000000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the)
i++;
n++;
}
}
for (int i = 0; i < n; i++) {
std::cout << array[i] << " ";
}
std::cout << "\n";
return 0;
}
I'm having problems figuring out where my bubble sort code went wrong. I'm almost positive it's in the sorting algorithm. Here is what I need my code to accomplish:
-Initialize an array a fill it with random numbers (I use getNumbers() to accomplish this)
-Compare the first element with all later elements. If the first element is larger, swap them.
-Compare the second element with all later elements one by one. If the second element is larger, swap them.
-Continue comparing and swap operations until the second to last element.
-Print out the sorted array
And here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int *, int *);
int *getNumbers(int);
int main()
{
//Get the size of the array from keyboard
int arraySize;
cout << "How many integers would you like to declare: ";
cin >> arraySize;
//Initialize array
int *array;
array = getNumbers(arraySize); //getNumbers should return a pointer
//Print out original array
cout << "Original array" << endl;
for(int count = 0; count < arraySize; count++)
{
cout << *(array + count) << " ";
}
//Sort array using the swap function
//Have a for loop to swap numbers one by one from min to max
//Compare values using a second for loop
//Swap values if former value is larger
//swap(&array[i],&array[j]);
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < (arraySize - 1); j++)
{
if(array[j] > array[j + 1])
{
swap(&array[i], &array[j]);
}
}
}
//Print out sorted array
cout << "\nSorted Array" << endl;
for(int count = 0; count < arraySize; count++)
{
cout << *(array + count) << " ";
}
return 0;
}
void swap(int *num1, int *num2)
{
//Keep record of original value of num1
int tempNum;
tempNum = *num1;
*num1 = *num2; //num1 value has been changed
*num2 = tempNum; //Fetch the original value of num1 and assign it to num2
}
int *getNumbers(int size)
{
int *array;
array = new int[size];
srand(time(0));
for(int i = 0; i < size; i++)
{
array[i] = rand() % 100;
}
return array;
}
Here is the correct code.
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int *, int *);
int *getNumbers(int);
int main() {
//Get the size of the array from keyboard
int arraySize;
cout << "How many integers would you like to declare: ";
cin >> arraySize;
//Initialize array
int *array;
array = getNumbers(arraySize); //getNumbers should return a pointer
//Print out original array
cout << "Original array" << endl;
for (int count = 0; count < arraySize; count++) {
cout << *(array + count) << " ";
}
//Sort array using the swap function
//Have a for loop to swap numbers one by one from min to max
//Compare values using a second for loop
//Swap values if former value is larger
//swap(&array[i],&array[j]);
for (int i = 0; i < arraySize; i++) {
for (int j = 0; j < (arraySize - 1); j++) {
if (array[j] > array[j + 1]) {
/*********** This line was changed ***********/
swap(&array[j+1], &array[j]); // You were earlier swapping ith and jth entries.
/*********************************************/
}
}
}
//Print out sorted array
cout << "\nSorted Array" << endl;
for (int count = 0; count < arraySize; count++) {
cout << *(array + count) << " ";
}
return 0;
}
void swap(int *num1, int *num2) {
//Keep record of original value of num1
int tempNum;
tempNum = *num1;
*num1 = *num2; //num1 value has been changed
*num2 = tempNum; //Fetch the original value of num1 and assign it to num2
}
int *getNumbers(int size) {
int *array;
array = new int[size];
srand(time(0));
for (int i = 0; i < size; i++) {
array[i] = rand() % 100;
}
return array;
}
You were swapping array[i] with array[j] in line 32. array[j] and array[j+1] should be swapped. Also, as pointed out by dd2, your loop bounds are not strict. The code would work correctly nonetheless but would take more steps. You can change the bound to j < (arraySize - i - 1)
Your loop bounds are not correct and swapping was wrong as well.
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < (arraySize - i - 1); j++)
{
if(array[j] > array[j + 1])
{
swap(&array[j], &array[j+1]);
}
}
}