Thread: How close is my program to an ideal modern C++ program?

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308

    How close is my program to an ideal modern C++ program?

    Hello!

    So, I recently started watching videos on YouTube by OneLoneCoder (Javidx9). He's really great in the way he explains things out about modern C++.

    Here's a few links:

    YouTube

    https://github.com/OneLoneCode

    OneLoneCoder.com - HOME


    In one of his 8 BITS OF TIPS videos, he suggested to practice from the most basic of C++ algorithms such as messing around with arrays and other stuff to learn more about modern C++.

    I just wrote a few lines of code doing the most basic of things. On a scale of 1 to 10, how close is this to an ideal program written in modern C++ (I'm guessing pretty low).

    I would love it if someone could direct/brief me with how to approach this, a step by step guide on how I should progress through my C++ learning journey, what topics should I finish before approaching something new. Any re-directions to another website would be cool to so long as I have more content to read and learn (history, functionality, uses, optimization, etc)

    Here's the code that I mentioned of:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    float Sum_Of_All_Elements     (int* , int);
    float Product_Of_All_Elements (int* , int);
    float Sum_In_A_Range          (int* , int , int);
    float Product_In_A_Range      (int* , int , int);
    float Minimum_In_A_Range      (int* , int , int);
    float Maximum_In_A_Range      (int* , int , int);
    void  Display_A_Range      (int* , int , int);
    
    
    int main()
    {
        int iMy_Arr[] = { 1,2,3,4,5 };
    
    
        cout << endl << Sum_Of_All_Elements(iMy_Arr , 5);
    
    
        cout << endl << Product_Of_All_Elements(iMy_Arr , 5);
    
    
        cout << endl << Sum_In_A_Range(iMy_Arr , 1 , 3);
    
    
        cout << endl << Product_In_A_Range(iMy_Arr , 1 , 3);
    
    
        cout << endl << Minimum_In_A_Range(iMy_Arr , 0 , 4);
    
    
        cout << endl << Maximum_In_A_Range(iMy_Arr , 0 , 4);
    
    
        Display_A_Range(iMy_Arr , 0 , 4);
    
    
        return 0;
    }
    
    
    float Sum_Of_All_Elements (int* iARRAY , int iSize = 0)   // Size: Sum till which element?
    {
        float fSum = 0;
    
    
        for (int iIndex = 0 ; iIndex < iSize ; iIndex++)
            fSum += iARRAY[iIndex];
    
    
        return fSum;
    }
    
    
    float Product_Of_All_Elements (int* iARRAY , int iSize = 0) // Size: Product till which element?
    {
        float fProduct = 1;
    
    
        for (int iIndex = 0 ; iIndex < iSize ; iIndex++)
            fProduct *= iARRAY[iIndex];
    
    
        return fProduct;
    }
    
    
    float Sum_In_A_Range (int* iARRAY , int iStart_Pos = 0 , int iEnd_Pos = 0) // iStart_Pos , iEnd_Pos: Which Indexes?
    {
        float fSum = 0;
    
    
        for (int iIndex = iStart_Pos ; iIndex <= iEnd_Pos ; iIndex++)
            fSum += iARRAY[iIndex];
    
    
        return fSum;
    }
    
    
    float Product_In_A_Range (int* iARRAY , int iStart_Pos = 0 , int iEnd_Pos = 0) // iStart_Pos , iEnd_Pos: Which Indexes?
    {
        float fProduct = 1;
    
    
        for (int iIndex = iStart_Pos ; iIndex <= iEnd_Pos ; iIndex++)
            fProduct *= iARRAY[iIndex];
    
    
        return fProduct;
    }
    
    
    float Minimum_In_A_Range (int* iARRAY , int iStart_Pos = 0 , int iEnd_Pos = 0) // iStart_Pos , iEnd_Pos: Which Indexes?
    {
        float fMin = iARRAY[iStart_Pos];
    
    
        for (int iIndex = iStart_Pos ; iIndex <= iEnd_Pos ; iIndex++)
            if (iARRAY[iIndex] < fMin)
                fMin = iARRAY[iIndex];
    
    
        return fMin;
    }
    
    
    float Maximum_In_A_Range (int* iARRAY , int iStart_Pos = 0 , int iEnd_Pos = 0) // iStart_Pos , iEnd_Pos: Which Indexes?
    {
        float fMax = iARRAY[iStart_Pos];
    
    
        for (int iIndex = iStart_Pos ; iIndex <= iEnd_Pos ; iIndex++)
            if (iARRAY[iIndex] > fMax)
                fMax = iARRAY[iIndex];
    
    
        return fMax;
    }
    
    
    void Display_A_Range (int* iARRAY , int iStart_Pos = 0 , int iEnd_Pos = 0) // iStart_Pos , iEnd_Pos: Which Indexes?
    {
        cout << endl;
    
    
        for (int iIndex = iStart_Pos ; iIndex <= iEnd_Pos ; iIndex++)
            cout << iARRAY[iIndex] << " ";
    }
    I have about 14 months of programming experience because of school... We started learning on TURBO C++ but as we progressed and learnt more, I started getting ahead of class by doing more research and have shifted to Code::Blocks for all coding purposes recently (except school projects as they're require to be done only in TURBO C++).

    ----------------------------------------------------------------------------

    On a side note, I've given rise to a problem on my C::B Output Window. I ran one of the programs provided by OLC (Javidx9) which involves the usage of <windows.h> header file. Before running the program, I always had a scroll-able Output Window but now I have a non-scroll-able Window which is affecting the way I can view my Output. It's a non-scroll-able window for all new files/projects, the reason for which I'm not aware of. If someone knows how to fix it, I'd really appreciate some help. Thanks!

  2. #2
    Guest
    Guest
    A good free introduction to C++ is learncpp.com. The chapters are numbered, so you can just follow the designated order. For a video series covering a multitude if C++ topics, check out TheChernoProject.

    On a scale of 1 to 10, how close is this to an ideal program written in modern C++ (I'm guessing pretty low).
    Well, with the exception of std::cout, your code is entirely C.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I just wrote a few lines of code doing the most basic of things. On a scale of 1 to 10, how close is this to an ideal program written in modern C++ (I'm guessing pretty low).
    Well since that code is really mostly C (other than the use of cout) I'd have to say less than 1.

    Modern C++ would be using std::vector instead of the raw arrays (or some other standard container), using std::algorithm, range based loops, smart pointers, etc.

    Also "encoding" the type of variable in a variable's name (fmax) is considered a bad practice in both modern C++ and modern C. You have already started using small simple functions so your variable names are easily visible and shouldn't need the "help".

    Lastly, for now, it is considered a better practice in both modern C++ and modern C to name the parameters in your prototypes. The prototypes should basically be identical to the function implementation.

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Thanks for the tips, jim. I'm yet to learn std::vector at school but as I'm ahead of class, I do have some idea about it. I will work on learning more about std::algorithm and smart pointers. After posting the thread, I downloaded a 344 page guide which I look forward to completing over the week.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Oh, I see. It's saddening to know that I've been learning and implementing C in the name of C++ at school. Thanks for pointing that out Adrian. Also, thanks for the link. Will be going through it right away.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Oh, I see. It's saddening to know that I've been learning and implementing C in the name of C++ at school.
    Well you said that you are required to use Turbo Crap in school, that you're really learning C doesn't surprise me at all. Turbo Crap was released before C++ was ever standardized so most code using it is really more like C with classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to close its own window
    By wizard21212 in forum C Programming
    Replies: 5
    Last Post: 04-20-2011, 11:57 PM
  2. How to Close Parent Program using child Program?
    By beon in forum Windows Programming
    Replies: 2
    Last Post: 11-30-2007, 10:24 PM
  3. Close Applications from C program...
    By manudathg in forum C Programming
    Replies: 11
    Last Post: 10-04-2005, 04:21 PM
  4. Detecting program close
    By HybridM in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2004, 10:08 AM
  5. Program doesn't close.
    By kdc6794 in forum Windows Programming
    Replies: 3
    Last Post: 12-10-2002, 02:07 AM

Tags for this Thread