Home > Blockchain >  the value is different inside and outside the for-loop C
the value is different inside and outside the for-loop C

Time:06-03

I am a beginner in C , and I want to write a program that replaces the max value with the min value in an array and vice versa.

    #include<iostream>
    using namespace std;
    int main(){

    int num, arr[200],max,min,max_pos,min_pos;

    cout <<"enter array size: ";
    cin >> num;

    for (int i=0; i<num; i  ){
        cout<<"Enter a value in array position "<<i<<" : ";
        cin>>arr[i];
    }

    for(int i=1, max=arr[0], min=arr[0]; i<num; i  ){
        if(arr[i]>arr[i-1] && arr[i]>max){
            max = arr[i];
            max_pos = i;
        }

        if(arr[i]<arr[i-1] && arr[i]<min){
            min = arr[i];
            min_pos = i;
        }
    
    cout<<"max_in "<<max<<"||"<<"min_in "<<min<<endl;
    }

    cout<<"max_out "<<max<<"||"<<"min_out "<<min<<endl;



    // arr[min_pos] = max;
    // arr[max_pos] = min;
    // cout<<"max: "<<arr[min_pos]<<" || min: "<<arr[max_pos];

    // cout<<"array is ";
    // for(int i =0; i<num;i  ){
    //     cout<<arr[i];}
    }

this is the output

enter how many number you want inside :4
Enter a value in array position 0 : 2
Enter a value in array position 1 : 1
Enter a value in array position 2 : 4
Enter a value in array position 3 : 6
max_in 2||min_in1
max_in 4||min_in1
max_in 6||min_in1
max 561||min -1343285512

I don't know why the values change outside the loop, where is the mistake?

CodePudding user response:

Here

for (int i=1, max=arr[0], min=arr[0] ;...

You declare 3 variables, they are called i,max and min. max and min shadow the variables of same name declared outside of the loop.

Do not declare the variables of same name, but use the ones you already declared:

int max = arr[0];
int min = arr[0];
for(int i=1; i<num; i  ){

This is a mistake you could have avoided by reading compiler warnings: https://godbolt.org/z/aT753e4nh. Also initializing variables always, and only declare a variable when you can initialize it, would have reduced chances for this kind of mistakes.

CodePudding user response:

In this for loop

for(int i=1, max=arr[0], min=arr[0]; i<num; i  ){
    if(arr[i]>arr[i-1] && arr[i]>max){
        max = arr[i];
        max_pos = i;
    }

    if(arr[i]<arr[i-1] && arr[i]<min){
        min = arr[i];
        min_pos = i;
    }

cout<<"max_in "<<max<<"||"<<"min_in "<<min<<endl;
}

you declared local variables max and min of the block scope of the for loop

for(int i=1, max=arr[0], min=arr[0]; i<num; i  ){
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

that hide the variables with the same name declared in the block scope of the function main

int num, arr[200],max,min,max_pos,min_pos;
                  ^^^^^^^

You need to remove the redundant declarations in the for loop and initialize the variables max_pos and min_pos that you forgot to initialize

max=arr[0]; min=arr[0];
max_pos = 0; min_pos = 0;

for(int i=1; i<num; i  ){

Also the conditions in the if statements can be written simpler

    if( arr[i]>max){
        max = arr[i];
        max_pos = i;
    }
    else if( arr[i]<min){
        min = arr[i];
        min_pos = i;
    }

Pay attention to that there is standard algorithm std::minmax_element that can be used instead of the for loop.

For example

#include <algorithm>

//...

auto p = std::minmax_element( arr, arr   num );
if ( p.first != p.second ) std::iter_swap( p.first, p.second );

Here is a demonstration program.

#include <iostream>
#include <algorithm>

int main()
{
    enum { num = 10 };
    int arr[num] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for (size_t i = 0; i < num; i  )
    {
        std::cout << arr[i] << ' ';
    }
    std::cout << '\n';

    auto p = std::minmax_element( arr, arr   num );
    if (p.first != p.second) std::iter_swap( p.first, p.second );

    for (size_t i = 0; i < num; i  )
    {
        std::cout << arr[i] << ' ';
    }
    std::cout << '\n';
}

The program output is

0 1 2 3 4 5 6 7 8 9
9 1 2 3 4 5 6 7 8 0
  • Related