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;
}
Related
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?
Ehab has an array a of length n. He has just enough free time to make a new array consisting of n copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence?
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order.
heres my answer, I keep getting "the system cannot find the file specified"
whats wrong?
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int t, n;
cin >> t;
while(t != 0){
cin >> n;
int* a = new int [n];
cin >> a[0];
int min = a[0];
for (int i = 1 ; i < n ; i++){
cin >> a [i];
if (min > a[i])
min = a[i];
}
int* b = new int [n*n];
for (int i = 0 ; i < n*n ; i++)
copy(a[0], b[n-1], b[i*n]);
int num = 0;
for (int i = 0 ; i < n*n ; i++)
if (b[i] == min){
for (int j = i+1 ; j < n*n ; j++){
if (b[j] > b[i]){
num++;
j = i;
}
break;
}
}
cout << num << endl;
t--;
}
}
I have a task to square all the elements of array, separate them with ",", then find a sum of the squared array and find the biggest number of it. I managed to square them and find the sum, but I can't find the biggest number and the program is also printing "," at the end of new array.
This is my code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n,sum=0,kiek=0,max=a[0];;
cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];
a[i]*=a[i];
sum=sum+a[i];
}
for (int i = 0 ; i < n ; i++)
{ cout <<a[i] << ","; }
cout<<endl ;
cout<<"suma " <<sum;
cout<<endl;
for(int i=0;i<10;i++)
{if(max<a[i])
{
max = a[i];
}
}
cout<<"max "<<max;
return 0;
}
This is the screenshot of my program result when I run it
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n, sum = 0; // Remove some unused variables
// Input //
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
a[i] *= a[i];
sum += a[i];
}
// List a[] and sum //
for (int i = 0 ; i < n - 1 ; i++) {
cout << a[i] << ", ";
}
cout << a[n - 1] << endl; // Just for a little beauty
cout << "suma " << sum << endl;
// Find Max //
int max = a[0]; // max should be declared there,
// because a[0] has not entered data at the first
for(int i = 0; i < n; i++) { // use n, not 10
if(a[i] > max){
max = a[i];
}
}
cout << "max " << max;
return 0;
}
Unchecked.
Please add indents, spaces and comment, this is a good habit.
Comment : If you are going to get the size of the array at run-time, it is better to use STL containers or pointers. Your issue lies here :
---> for(int i=0;i<10;i++)
{if(max<a[i])
Good luck.
I cannot display the index containing the highest value of integer. but i can display the highest value of integer in the array.
Here's my code:
#include <iostream>
#include<stdlib.h>
using namespace std;
int main () {
int a[10], highest,temp = 0;
do{
cout<<"Enter 10 Numbers: ";
cin>>a[temp];
temp++;
}while(temp !=10);
for(int j = 0; j <10; j++){
if(a[0]<a[j]){
highest = j;
}
}
for(int x = 0; x <10; x++){
if(a[0]<a[x]){
a[0] = a[x];
}
}
cout<<"The highest number is "<<a[0] <<" at index "<<highest<<endl;
system("pause");
return 0;
}
For starters do not use magic numbers as for example the number 10 in your program. Use named constants instead.
Also declare variables in the minimum scope where they are used.
This loop
for(int j = 0; j <10; j++){
if(a[0]<a[j]){
highest = j;
}
does not make sense because it searches the last element in the array that is greater than the element a[0]. It is not the same as searching the index of the greatest element.
Also as the variable highest is not initialized then in general the program has undefined behavior.
Instead of two loops you can use one loop that finds the index of the greatest element.
Here is a demonstrative program that shows how it can be done.
#include <iostream>
int main()
{
const size_t N = 10;
int a[N];
std::cout << "Enter " << N << " umbers: ";
for ( size_t i = 0; i < N; i++ ) std::cin >> a[i];
size_t max = 0;
for ( size_t i = 1; i < N; i++ )
{
if ( a[max] < a[i] ) max = i;
}
std::cout << "The highest number is " << a[max]
<< " at index " << max << std::endl;
return 0;
}
Its output might look like
Enter 10 umbers: 7 2 3 0 9 1 8 6 4 5
The highest number is 9 at index 4
Take into account that there is standard algorithm std::max_element declared in the header <algorithm> that finds the maximum element in a sequence and returns iterator/pointer to it.
For example
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
const size_t N = 10;
int a[N];
std::cout << "Enter " << N << " umbers: ";
for ( size_t i = 0; i < N; i++ ) std::cin >> a[i];
int *max_value = std::max_element( a, a + N );
std::cout << "The highest number is " << *max_value
<< " at index " << std::distance( a, max_value ) << std::endl;
return 0;
}
The program output might look as shown above
Enter 10 umbers: 7 2 3 0 9 1 8 6 4 5
The highest number is 9 at index 4
The first part of your code detects the highest index of an element that is larger than the first element, not of the element that has the highest value of all:
for(int j = 0; j <10; j++){
if(a[0]<a[j]){
highest = j;
}
}
This is because you always compare to a[0] (which does not change), rather than to the highest value reached so far.
The second loop for finding the highest value itself, in contrast, works because you change a[0] to the "local" maximum found so far.
for(int x = 0; x <10; x++){
if(a[0]<a[x]){
a[0] = a[x]; // remembers the maximum found so far for further comparisons
}
}
You could easily combine both as follows. Note (as a minor thing) that it is sufficient to start from 1:
int indexOfMax = 0;
for(int x = 1; x <10; x++){
if(a[0]<a[x]){
a[0] = a[x]; // remembers the maximum found so far for further comparisons
indexOfMax = x; // stores the index of this element.
}
}
As long as you are storing the highest value in a[0] you need to swap it with the new greater values if found after each iteration a bit of some code in your program should look like:
for(int j = 0; j <10; j++){
if(a[0]<a[j]){
int tmp = a[0];
a[0] = a[j];
a[j] = tmp;
highest = j;
}
}
A simple, readable example looks like:
int a[10], highest = -1, highestIndex = -1;
for(int i(0); i < 10; i++){
cout<<"a[" << i << "]: ";
cin >> a[i];
}
for(int i = 0; i < 10; i++){
if(a[i] > highest){
highest = a[i];
highestIndex = i;
}
}
cout << "The highest number is " << highest << " at index " << highestIndex << endl;
Please don't ask for a while-do loop you can handle it.
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]);
}
}
}