Thread: Stuck at arrays

  1. #16
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    I know I'm a newbie but the variable size in his code doesn't have specified value nor is defined anywhere in the snippet. I reckon it won't run at all or it'll throw an error.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The exercise says one function returns the highest value and the second returns the lowest.
    Part of your problem is that in your original code you're not finding the highest or lowest values, you're attempting to find the "index" to the highest and lowest values, along with the problem of not initializing your "minimum" and "maximum" variables.

    With an array that contains the values of 4,5,3,1 the highest value is 5 and the "index" would be 1 (it's the second element in the array). The lowest value would be 1 and it's "index" would be 3 (it's the fourth element in the array).

    Remember this array would be:

    array[0] == 4
    array[1] == 5
    array[2] == 3
    array[3] == 1

    So to find the maximum value something like:
    Code:
    int theGreatestNumber(int array[], int size)
    {
       int max = array[0];  // Note that max is initialized with the value of the first element of the array.
       for (int i = 1; i < size; i++)
       {
          if (array[i] > max)
          {
             max = array[i];
           }
       }
       return max;  // Note the placement of the return statement.
    }
    I know I'm a newbie but the variable size in his code doesn't have specified value nor is defined anywhere in the snippet. I reckon it won't run at all or it'll throw an error.
    The variable "size" is initialized in the calling function and passed to this function as a parameter.

    Jim

  3. #18
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    Jim, your code seems to be working !

    Perhaps you could explain me why from this array:

    Code:
    int array[] = {9, 1, 45, 3, 90};
    I'm getting this output:

    Stuck at arrays-imageedit_2_8036396067-jpg

    Would you explain me why the compiler outputting numbers I don't have in my code ?

  4. #19
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    @jimblumberg
    By the way @OldGuy2 how may times do you think that loop will run?
    Only once, somehow the closing brace ended up in the wrong place.

    @OP,
    looks like you output elements from behind your array which has garbage values.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Perhaps you could explain me why from this array:
    Please post the complete program.

    Jim

  6. #21
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    @OldGuy2
    looks like you output elements from behind your array which has garbage values.
    Is there anything that is hinting me about code that gives gibberish output ?


    Code:
    int theGreatestNumber(int array[]) {
    
    
        int max = array[0];
        for (int i = 1; i < sizeof(array); i++) {
            if (max < array[i]) {
                max = array[i];
            }
    
    
       }
        return max;
    }
    
    
    int theLowestNumber(int array[]) {
    
    
        int min = array[0];
        for (int i = 1; i < sizeof(array); i++) {
            if (min > array[i]) {
                min = array[i];
            }
        }
        return min;
    }
    
    
    
    
    void displayArray (int array[]) {
    
    
        std::cout << "{";
        for (int i = 0; i < sizeof(array); i++) {
    
    
            if (i != 0) {
                std::cout << ", ";
        }
        std::cout << array[i];
        }
        std::cout << "}";
    }
    
    
    
    
    int main() {
    
    
    
    
        int array[] = {9, 1, 45, 3, 90};
    
    
        std::cout << "The greatest number is: ";
        std::cout << theGreatestNumber(array);
        std::cout << "\n";
        std::cout << "\n";
        std::cout << "The lowest number is: ";
        std::cout << theLowestNumber(array);
        std::cout << "\n";
        std::cout << "\n";
        std::cout << "Array: ";
        displayArray(array);
        std::cout << "\n";
    
    
    }

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well first off why did you remove the "size" parameter from your functions? You need this variable or else your functions won't work properly.

    The sizeof() function will only help you find the size of an array when the array definition is in scope. When you pass that array into the function you're really passing a pointer to the array, not the array it's self. In those functions the sizeof() function will tell you how many bytes it takes to hold your pointer, which looks like 8 bytes on your computer.

    So put the size parameters back in your functions and pass the correct size of the array into these functions from main().

    Jim

  8. #23
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    @jimblumberg

    Dammit ... you were right. As soon as I put those variables back it worked wonderfully. Can't thank you enough for having the nerve to explain me how it works. Cheers !

    If it's not a secret ... what you do ? Do you work as a software engineer for some company ? Clearly you know what you're doing.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If it's not a secret
    Not a secret, just not relevant.

    Do you work as a software engineer for some company ?
    Not anymore.

    Clearly you know what you're doing.
    Thanks.

    By the way one of the benefits of using a std::vector over an array is that a vector always knows it's size, and the other big advantage is that a vector is dynamic, meaning that it can grow and shrink as the situation demands. This is an example of your program using a std::vector, with varying amount of more modern C++ elements. IMO, that book your reading is not really the best book to learn current C++.

    Code:
    #include <iostream>
    #include <vector>
    
    int theGreatestNumber(const std::vector<int>& my_array)
    {
        int biggest = my_array[0];
        for(size_t i = 1; i < my_array.size(); i++)
        {
            if(my_array[i] > biggest)
                biggest = my_array[i];
        }
        return biggest;
    }
    
    int theSmallestNumber(const std::vector<int>& my_array)
    {
        int smallest = my_array[0];
        for(auto& itr : my_array)
        {
            if(itr > smallest)
                smallest = itr;
        }
        return smallest;
    }
    
    void displayArray(const std::vector<int>& my_vector)
    {
        std::cout << "{";
    
        for(size_t i = 0; i < my_vector.size(); i++)
        {
            if(i != 0)
            {
                std::cout << ", ";
            }
    
            std::cout << my_vector[i];
        }
    
        std::cout << "}\n";
    }
    
    
    int main()
    {
        std::vector<int> my_vector{5, 7, 3, 6};
    
        displayArray(my_vector);
    
        std::cout << theGreatestNumber(my_vector) << std::endl;
        std::cout << theSmallestNumber(my_vector) << std::endl;
    }
    Jim

  10. #25
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    IMO, that book your reading is not really the best book to learn current C++.
    Nice of you for saying that since the book is the most advertised here. LOL. I admit the book might be a bit outdated but at least something useful will take away from it. If anything it will get my brain to think about the language (eventually). Besides the book has very positive reviews on Amazon so I went for it. It can't hurt.

    Thanks for the code btw. Will have a look over it.
    Last edited by markUK; 07-01-2017 at 03:17 PM.

  11. #26
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Quote Originally Posted by markUK View Post
    Nice of you for saying that since the book is the most advertised here. LOL.
    You can't blame the site owner for advertising his own book, can you ?

    Anyway you could skip the chapters till the STL and come back for pointers and dynamic memory later.

  12. #27
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    Quote Originally Posted by OldGuy2 View Post
    Anyway you could skip the chapters till the STL and come back for pointers and dynamic memory later.
    Yeah I'll continue despite not having all problems solved. For example I've been trying to come up with sorting algorithm for an array and nothing worked so far. And I've already spent huge chunk of time just on this. Not really productive and can't really talk about learning experience either. I'm in a third of the book and wanna learn more. Being stuck on algorithms is killing me. I know you can't run before you can walk but I'll roll there instead of walking.
    Last edited by markUK; 07-02-2017 at 02:43 AM.

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For example I've been trying to come up with sorting algorithm for an array and nothing worked so far. And I've already spent huge chunk of time just on this.
    What do you mean by this? I hope you mean you've been searching the internet for information about sorting in C++ and not that you're trying to design your own sort routine. If you do mean you've been searching the internet then I wouldn't consider that a waste of time and it would be a productive learning experience in my opinion. But trying to design your own sort routine would be a waste of time and effort unless you happen to be a mathematician.

    Jim

  14. #29
    Registered User
    Join Date
    Jul 2017
    Posts
    14
    Actually that's exactly what I meant. I use other people's work only if I can't think of solution on my own. Which can be frustrating sometimes. When you're trying hard to make it work but compiler doesn't like it
    I think it develops thinking skills if you're trying to figure things out alone first. I also think this approach makes a best hacker

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually that's exactly what I meant. I use other people's work only if I can't think of solution on my own. Which can be frustrating sometimes.
    Frustration would be normal in this situation and it really doesn't make much sense. You're already using other people's work since you're reading a book and following examples presented in the book.

    Looking for information about language constructs and algorithms on the internet is not "cheating" just as going to a physical "library" to research these topics is not "cheating". It would only be "cheating" if took some random bit of code that you don't understand and "pretend" that it is your work that you fully understand.

    Part of being a good programmer will be knowing where and how to find information to help you solve a problem.

    I also think this approach makes a best hacker
    Really? So how do you define a "hacker"?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character arrays (basics, stuck on applying the tutorial)
    By rwebb2305 in forum C++ Programming
    Replies: 10
    Last Post: 07-01-2011, 08:39 PM
  2. stuck on my first day of C coding (arrays)
    By viper2k in forum C Programming
    Replies: 7
    Last Post: 03-27-2011, 06:09 AM
  3. New to C++ Stuck on Arrays. Help please!
    By PersianStyle in forum C++ Programming
    Replies: 5
    Last Post: 07-15-2009, 01:45 AM
  4. Stuck, arrays
    By NoobieGecko in forum C Programming
    Replies: 26
    Last Post: 02-17-2008, 12:14 AM
  5. Stuck again: Arrays
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 12:37 AM

Tags for this Thread