Array Operations C++ Tutorial menu

Shout-Out

User Tag List

Results 1 to 1 of 1
  1. #1
    BitHacker's Avatar Master Sergeant
    Reputation
    13
    Join Date
    May 2012
    Posts
    114
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Operations C++ Tutorial

    I wanted to put this on the forum for anyone who is learning c++. Arrays are special. When dealing with arrays you have to make sure you pay attention to the element index and element value. Sometimes these two get mixed up and blurred together for someone that is learning. I hope this helps in peering into array operations.

    C++ has no guard against out of bounds indices. So be careful. Always check to make sure your operations aren't splattering your memory with your elements

    Pay special attention to finding the largest and smallest elements, you will then see why you need to keep track of your element index and element value!


    Code:
      
    /* main.cpp
    *
    *   Copyright 2012 Bit_Hacker
    *
    *    Array Operations
    *    ----------------
    *   1 - Initializing each element
    *   2 - Inputting data into each element
    *   3 - Outputting each elements data
    *   4 - Finding the largest element
    *   5 - Finding the smallest element
    *
    *    --- NUMERICAL OPERATIONS ---
    *   6 - Sum of elements
    *   7 - Average of elements
    */
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        // 1) Initializing each element
        double sales[10];
        int index;
        for (index = 0; index < 10; index++)
            sales[index] = 0.0;
    
    
    
        // 2) Inputting data into each element
        for( index = 0; index < 10; index++ )
            cin >> sales[index];
    
    
    
        // 3) Outputting each elements data
        for( index = 0; index < 10; index++ )
            cout << sales[index] << " ";
    
    
    
        // 4) Finding the largest element
        // Assuming maxIndex is the first occurence of the largest element.
        int maxIndex = 0;
        double largestSale;
        for( index = 1; index < 10; index++ )
        {
            if( sales[maxIndex] < sales[index] )
                maxIndex = index;
        }
        largestSale = sales[maxIndex];
    
    
    
        // 5) Finding the smallest element
        // Assuming minIndex is the first occurence of the smallest element.
        int minIndex = 0;
        double smallestSale;
        for( index = 1; index < 10; index++ )
        {
            if( sales[minIndex] > sales[index] )
                minIndex = index;
        }
        smallestSale = sales[minIndex];
    
    
    
        /* --- NUMERICAL DATA OPERATIONS BELOW --- */
    
        // 6) Sum of elements
        double sum = 0;
        for( index = 0; index < 10; index++ )
            sum = sum + sales[index];
    
    
    
        // 7) Average of elements
        double average;
        for( index = 0; index < 10; index++ )
        {
            sum = sum + sales[index];
        }
        average = sum / 10;
    
    
    
        cin.ignore(2);
        
        return 0;
    }
    -Bit_Hacker

    Array Operations C++ Tutorial

Similar Threads

  1. Exploting tutorial and top elwyn bugs. Video
    By kp3 in forum World of Warcraft Exploits
    Replies: 5
    Last Post: 09-17-2006, 10:53 PM
  2. Ontop of SW wall(first gate) 1.12 -pictures+video tutorial.
    By kp3 in forum World of Warcraft Exploits
    Replies: 15
    Last Post: 09-16-2006, 04:49 PM
  3. Full Old School Tutorial...
    By Gnaughty in forum World of Warcraft Exploration
    Replies: 11
    Last Post: 09-12-2006, 10:38 AM
  4. Game Hacking Tutorial!
    By lopolop in forum Community Chat
    Replies: 24
    Last Post: 06-29-2006, 08:39 AM
  5. Muling Tutorial
    By Matt in forum World of Warcraft Guides
    Replies: 0
    Last Post: 06-19-2006, 05:40 PM
All times are GMT -5. The time now is 01:35 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search