-1

I need to find the minimum value in int array, but the answer it gives me is really odd and from that I cannot even judge where the error is, I would really appreciate help. Here is the code I have written:

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <conio.h>
#include <random>
#include <cstdlib>
#include <ctime>

using namespace std;

int minVert(int minimum, int i) {
    int j, array[20];
    for (j = 0; j < 19; j++) {
        if (array[i] < minimum ) {
            minimum = array[i];
        }
    }
    return minimum;
}

int main(int k, int minimum) {
    int array[20];
    srand(time(NULL));
    for (int i = 0; i < 20; i++) {
        k = rand() % 1000 + 2;
        array[i] = k;
    }
    cout << "Tiek generets masivs..." << endl << "..." << endl << "..." << endl << "..." << endl << endl;
    cout << "Masiva elementi ir: " << endl;
    for (int j = 0; j <20; j++) {
        cout << array[j] << endl;
    }
    cout << endl << "Masiva mazaka vertiba ir: " << endl << minimum << endl << endl;
    cout << "Nospiediet jebkuru taustinu, lai izietu no programmas" << endl;
    _getch();
    return 0;
}
theoden8
  • 773
  • 7
  • 16
Royal
  • 41
  • 8
  • So what's the result you're receiving. Please post it as well as the input – ForceBru Sep 19 '15 at 16:46
  • 2
    I don't see any initialization for `minimum`, and why do you have `j < 19` in your first `for` loop when your array has 20 elements? – Logicrat Sep 19 '15 at 16:48
  • 7
    Oh WTF you're passing `minimum` to main??! The arguments of main should be `int argc, char **argv`. If you wanna pass minimum as an argument to your prog, you want to use argv & argc like shown [here](http://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm) – ForceBru Sep 19 '15 at 16:48
  • The result is random, as is the array's contents generated at random, one of the results is: 2653742 – Royal Sep 19 '15 at 16:53
  • 1
    haha, `fatal error: 'conio.h' file not found`. – theoden8 Sep 19 '15 at 17:17
  • You have haphazardly included a combination of C and C++ headers. You shouldn't mix the two. – PC Luddite Sep 19 '15 at 17:23
  • I am still learning and kind of gotten into a few chaotic problems. I am sorry for the sloppy code. – Royal Sep 19 '15 at 17:25
  • *"I need to find the minimum value in int array"* - `std::min_element` works just fine with arrays. – Christian Hackl Sep 19 '15 at 17:51
  • cout << "minumum is : " << min_element(array,array+20) << endl; doesn't work, just shows as result - 0013FCC0 – Royal Sep 19 '15 at 18:02

3 Answers3

4
 int main(int k, int minimum){

This is illegal. The only valid signature for main() is

int main(int argc, char* argv[]) {
    // ...
}  

or leave out any parameters at all

int main() {
    // ...
}

In the first case argc contains the number of command line arguments passed, and argv is an array of char* pointers, that contain the arguments.

Note that argv[0] contains the name of the executed program itself.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

I think your trying to populate the array with random numbers and then find the minimum.

First remove those parameters from the main function if your not going to use them. You haven't really called the function you wrote in the main. so either write that inside the main or call that function. i would suggest this :

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    srand(time(NULL));
    int array[5];
    int minimum;

    for(int i = 0; i < 20; i++)
    {
        array[i] = rand() % 100 + 2;
    }

    for(int i = 0; i < 20; i++)
    {
        cout << array[i] << endl;
    }

    minimum = array[0];

    for(int i = 0; i < 20; i++)
    {
        if(array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    cout << "minumum is : " << minimum;
}
theoden8
  • 773
  • 7
  • 16
Yomal
  • 362
  • 2
  • 15
0

Initialize your minimum variable as minimum = array[0] so your code will become like follow

int minVert(int minimum, int i){

    int j, array[20];
    minimum = array[0];
    for (j = 0; j < 19; j++) {
       if (array[i] < minimum ) {
           minimum = array[i];
       }
    }
    return minimum;
}

And if you want to write a function to find the minimum from an array the best practice is to write a code which will take reference of the array and return the minimum value

dnup1092
  • 375
  • 4
  • 15