-2

I know this is a basic problem, but I don't know C that well and my teacher doesn't teach and I thought maybe stack overflow can help me out and teach me. Especially since I have an exam coming up and most likely this concept will be on the exam. I am clueless.

So this is my assignment:

And this is my code:

#include <stdio.h>
#define ARRSIZE 6

int main() {
    int arrOfInt[ARRSIZE] = {19, 51, 23, 6, 79, 95};
    int i;
    int min;
    int INT_MIN;
    
    min = INT_MIN;

    for (i = 0; i < ARRSIZE; i++) {

        if(arrOfInt[i] < min) {

            min = arrOfInt[i];

        }

        }

    }

    printf("Minimum is: %d\n", min);

    return 0;
}

I got started and probably did it wrong, but I don't know what to do next.

Also he said "write it from scratch" and I don't know what that means. I'm assuming he means no libraries or anything like that? I'm not entirely sure but I'm assuming what I'm doing is "from scratch."

Also if you don't want to tell me how to do the max number because you're afraid of giving me the answer, then that's fine you don't have to because once I know how to do this for the minimum, I'll know how to do it for the max and will be able to do it on my own (I think.)

Thank you, any help would be appreciated!

EDIT: So I'll make it more clear and edit the code a bit. I guess this question will turn into me asking how to fix a series of errors.

I fixed the code a bit and I'm left with this error:

main.c:5:29: error: expected expression before ‘[’ token int arrOfInt[ARRSIZE] = [19, 51, 23, 6, 79, 95];

EDIT 2: The error is fixed but now it outputs nothing.

EDIT 3: It inputs something but it only inputs 0. I decided to change the i = 0 to i = 100 but it didn't work.

mmno
  • 1
  • 6
  • 1
    Does this answer your question? [Finding smallest value in an array most efficiently](https://stackoverflow.com/questions/1042507/finding-smallest-value-in-an-array-most-efficiently) – Jake Grossman Oct 01 '20 at 04:20
  • The minimum part of this is fine, but there are errors in the surrounding code. In general, please include things like compiler errors in a question (in other words, share the reason you think it’s wrong). – Ry- Oct 01 '20 at 04:22
  • 3
    You never declared the variable `min`. – Barmar Oct 01 '20 at 04:24
  • 2
    What is the `if(arrOfInt)` conditional supposed to achieve? As to the logic, it happens that you get the logic the wrong way around. – ndim Oct 01 '20 at 04:25
  • 1
    Your test is backwards. You should check if the current element is smaller than `min`, then it's a new minimum. – Barmar Oct 01 '20 at 04:25
  • So you're actually calculating the max, not the min. – Barmar Oct 01 '20 at 04:25
  • 1
    Don't forget to give `min` an initial value when you declare it. And don't use `0` as the initial value, because that's already smaller than any of your array elements. – Barmar Oct 01 '20 at 04:26
  • @Barmar Hi again! I kinda embarrassed myself in my last post by thinking mmno is an actual name haha. Anyway, I fixed it up a bit and edited the question :) – mmno Oct 01 '20 at 04:44
  • @JakeGrossman Not really. It's not clear also it's C++ but I want C. Thank you tho! – mmno Oct 01 '20 at 04:45
  • @mmno: The current version of your code is mostly right; except that you need to use curly braces for the array's data (like `int arrOfInt[ARRSIZE] = {19, 51, 23, 6, 79, 95};`), the variable `min` is uninitialized and needs to be (like `int min = INT_MAX;`) and `if(arrOfInt) {` is redundant (you know the array exists and `arrOfInt` can't be NULL). – Brendan Oct 01 '20 at 04:53
  • @Brendan thank you! now it outputs without any error but it outputs nothing now. – mmno Oct 01 '20 at 04:56
  • @mmno: Ah - I missed that part (and focused on the "find the minimum"). To print the minimum just add something like `printf("Minimum is: %d\n", min);`" before the `return 0;`. – Brendan Oct 01 '20 at 05:01
  • @Brendan Thank you so much! Sorry for annoying you with all my errors but it only outputs 0, why is that? I changed the i = 0 to i = 100 assuming that would fix the error but it didn't. – mmno Oct 01 '20 at 05:21
  • 1
    `min` is still uninitialized. Starting `i` at 100 makes no sense, you are accessing the 100th element of the array which doesn't exist. Starting at `i=0` was correct, but the first time through the loop, you compare `arrOfInt[0]` with whatever garbage value happens to be in `min`. If it happens to be 0 then it will never change because 0 is less than every element of the array. I suspect maybe you meant to initialize `min` to 100 instead of `i`. – Nate Eldredge Oct 01 '20 at 05:22
  • You don't redeclare the constant `INT_MIN` with `int INT_MIN;`. `INT_MIN` is already defined. You simply `#include ` and use `INT_MIN` to initialize `int max = INT_MIN;` and `int min = INT_MAX;` to ensure you find the `min` if the array contains nothing large positive numbers and the `max` if it contains only large negative numbers. – David C. Rankin Oct 01 '20 at 05:52

1 Answers1

0

I managed to solve it. I'm going to post it as an answer just so I can mark it as solved.

I decided to read the slides, read the book more, and read the answers I had been given. I tried viewing it from a different POV and used pseudo-code to assist me and I finally solved it. I rewrote it because the previous code was a mess and I didn't wanna touch it anymore. (Oh and I did max too so yay!)

EDIT: Well looks like I have to wait two days to close this question, but I want to close it now. :(

#include <stdio.h>
#define ARRSIZE 6

int main() {

    int min_value, max_value, i;
    int arrOfNum[] = {19, 51, 23, 6, 79, 95};
    int length;

    min_value = max_value = arrOfNum[0];

    length = sizeof (arrOfNum) / sizeof (arrOfNum[0]);
   
     for(i = 1; i < length; i++) {
       
         if (arrOfNum[i] < min_value) {

        min_value = arrOfNum[i];

            }
     
           if (arrOfNum[i] > max_value) {

            max_value = arrOfNum[i];
        
                }
        }

    printf("Minimum value is %d\n", min_value);
    
    printf("Maximum value is %d\n", max_value);
}
mmno
  • 1
  • 6