user2840960
user2840960

Reputation: 49

Array Manipulation C++

I am trying to create a code that reads in an array, finds the smallest element in that array, and then subtracts that smallest element from all the other elements in the array and prints a new array with these elements.

The code I have written so far will not even compile. Here is what I have:

#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE(25);

void read_list(int a[], int&num_ele);
void print_array(const int a[], const int num_ele);
int find_min(const int a[], const int num_ele);
void array_subtract(int x, int&a[], int num_ele) ;



int main()
{
  int num_ele(0);
  int array[SIZE] ;

  read_list(array, num_ele);

  cout<<"Before list: ("<<num_ele<< " numbers): " <<endl;

  print_array(array, num_ele) ;

  min = find_min(array, num_ele) ;

  cout<<"The minimum value = "<<min<<endl;

  array_subtract(min ,array ,num_ele) ;

  cout<<"After list: ("<<num_ele<< " numbers): " <<endl;

  print_array(array, num_ele) ;


  return 0;
}


void read_list(int a[], int&num_ele)
{
  int x(0);

  cout<<"Enter positive numbers (ints) terminated 0: "<<endl;
  cin>>x;

  while(x!=0 && num_ele <= SIZE)
    {
      a[num_ele] = x ;
      num_ele++;
      cin >> x;
    }
}


void print_array(const int a[], const int num_ele)
{


  for(int i=0; i<num_ele; i++)
    {
      if(i<num_ele-1)
    {cout<<a[i]<<", "; }
      else
    {cout<<a[i]<<"."}
    }
}


int find_min(const int a[], const int num_ele)
{
  int x= a[0];
  for(int k=0; k<num_ele; k++)
    {
      if(a[k] < x)
    { x=a[k] ;}
    }

  return x ;
}

void array_subtract(int x, int&a[], int num_ele)
{
  for(int j=0; j<num_ele; j++)
    {
      a[j] = a[j]-x ;
    }
}

When I go to compile, at the line

  cout<<"The minimum value = "<<min<<endl;

I get about 100 lines of errors.

Is there some huge error in my code I am missing? Why would this happen? I would like to keep the code in the same format it is in (calling functions to get the final output).

Thanks

Upvotes: 1

Views: 4711

Answers (3)

David
David

Reputation: 28178

Aside from the compilation errors addressed by others...

Instead of using an array, use a vector (since logically you array is variably sized from 0-SIZE, and a vector is a variable sized array)...

vector<int> v;

Accept integer input until 0 is typed...

for(int n; cin >> n && n != 0;)
    v.push_back(n);

Find the min element...

auto min = min_element(begin(v), end(v));

Printing can be done a variety of ways...

cout << "Vector: [";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << "]\n";

Subtract the min from the elements...

transform(v.begin(), v.end(), v.begin(), [min](int n){ return n-min; });

Try to use the C++ standard library when possible instead of hand rolling loops. It reduces the potential for bugs, and is often clearer (but not always... for example that print I posted above doesn't exactly read intuitively IMO)

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

You did not define variable min so the compiler reports errors when encounters statement

min = find_min(array, num_ele) ;

Upvotes: 0

masoud
masoud

Reputation: 56479

You have 3 problems, after correcting these problems, at least you have not compile errors.

i.

You're passing arrays wrong:

void array_subtract(int x, int&a[], int num_ele);
                           ^^^^^^^

Pass it like below (remove &)

void array_subtract(int x, int a[], int num_ele);

ii.

You're using variable min without declaring it. Declare it like below:

int min;

iii.

You've missed a semicolon ; in function print_array:

cout<<a[i]<<".";
               ^

Upvotes: 2

Related Questions