Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.92 KB | None | 0 0
  1. // arrays and stuff.cpp : Defines the entry point for the console application.
  2. // set your worsecase/bestcase scenerio. 20% movement either way.
  3. // standard deviation
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <string>
  8. #include <conio.h>
  9. #include <fstream>
  10. #include <iomanip>
  11. #include <algorithm>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16.  
  17.  
  18.  
  19. void showValues (int [], int);                                                      // Function prototype. To define a function that takes an array parameter,
  20.                                                                                     // use empty [] for array argument.
  21. void lab8ArrayFiller (int [], int, int);
  22.  
  23. void lab9sort (char [], char [], char [], char [], int);
  24.  
  25. bool lab9alphabetical (char [], char [], int);
  26.  
  27. int _tmain(int argc, _TCHAR* argv[])
  28.  
  29. {
  30.     const int ARRAY_SIZE = 8;
  31.     int numbers[ARRAY_SIZE] = {5, 10, 15, 20, 25, 30, 35, 40};
  32.  
  33.     showValues (numbers, ARRAY_SIZE);                                               // To pass an array to a function, just use the array name. It is common to pass array size
  34.                                                                                     // so that function knows how many elements to process.
  35.  
  36.     cout << numbers[0] << endl;                                                     // Array names in functions are like reference variables - changes made to array in a function
  37.                                                                                     // are reflected in actual array in calling function.
  38.  
  39. /*
  40.     #define infile "numbertest.txt"
  41.     ifstream ins;
  42.     ins.open(infile);
  43.  
  44.  
  45.     int count = 0;
  46.     const int arraySize = 4;
  47.     double arrayCad [arraySize];
  48.     double arrayUsd [arraySize];
  49.     double arrayInt [arraySize];
  50.     double arrayBond [arraySize];
  51.  
  52.  
  53.     while (count < arraySize && ins >> arrayCad [count])
  54.         {
  55.             cout << arrayCad [count] << endl;
  56.             count = count + 1;
  57.         }
  58.  
  59.     count = 0;
  60.     cout << endl << endl;
  61.     while (count < arraySize && ins >> arrayUsd [count])
  62.         {
  63.             cout << arrayUsd [count] << endl;
  64.             count = count + 1;
  65.         }
  66.  
  67.     count = 0;
  68.     cout << endl << endl;
  69.     while (count < arraySize && ins >> arrayInt [count])
  70.         {
  71.             cout << arrayInt [count] << endl;
  72.             count = count + 1;
  73.         }
  74.  
  75.     count = 0;
  76.     cout << endl << endl;
  77.     while (count < arraySize && ins >> arrayBond [count])
  78.         {
  79.             cout << arrayBond [count] << endl;
  80.             count = count + 1;
  81.         }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.     ins.close();
  88.  
  89.     cout << endl << endl;
  90.     */
  91.  
  92.     //*******************************************************************************************
  93.     //sort(array1, array1 + arraySize);
  94.  
  95.     /*
  96.     while   (array1 [0] > array1 [1] || array1 [1] > array1 [2] || array1 [2] > array1 [3])
  97.         {
  98.             count = 0;
  99.             int temp = 0;
  100.             while (count < (arraySize - 1))
  101.             {
  102.             temp = array1 [count];
  103.  
  104.             if( array1 [count] > array1 [count + 1])
  105.                 {
  106.                 array1 [count] = array1 [count + 1];
  107.                 array1 [count + 1] = temp;
  108.                            
  109.                 }
  110.            
  111.             count = count + 1;
  112.             }
  113.         }
  114.     */
  115.     //*******************************************************************************************
  116.  
  117.     /*
  118.     #define infile2 "numbertest2.txt"
  119.     ifstream ins;
  120.     ins.open(infile2);
  121.  
  122.  
  123.     struct Asset
  124.     {
  125.         vector <double> allocation;     //asset allocation percentage
  126.         vector <string> name;           //fund name
  127.         vector <string> symbol;         //ticker symbol
  128.         vector <string> geo;            //geography
  129.  
  130.  
  131.     };
  132.  
  133.     struct Asset sector1;
  134.    
  135.  
  136.     while (!ins.eof())
  137.         {
  138.             double temp = 0;
  139.             ins >> temp;
  140.             sector1.allocation.push_back(temp);
  141.         }
  142.  
  143.  
  144.     sector1.allocation.resize(10);
  145.  
  146.     cout << sector1.allocation[0] << endl;
  147.     cout << sector1.allocation[1] << endl;
  148.     cout << sector1.allocation[2] << endl << endl;
  149.  
  150.  
  151.     sector1.geo.push_back("Canada");
  152.  
  153.     cout << sector1.geo[0] << endl << endl;
  154.     */
  155.    
  156.     ///////////////////////////////////////////////////////////////////
  157.     //An array of a structure
  158.     /*
  159.     Asset assortment[10];
  160.  
  161.     assortment[0].name.push_back("bassi");
  162.  
  163.     cout << assortment[0].name[0] << endl;
  164.     */
  165.  
  166.  
  167.  
  168.     ///////////////////////////////////////////////////////////////////
  169.     //lab 8. Write a program to find the largest odd number in a 50-element array of integers
  170.     /*
  171.     #define infile3 "lab8.txt"
  172.     ifstream ins;
  173.     ins.open(infile3);
  174.  
  175.  
  176.  
  177.     int lab8 [50];
  178.     int largest = 0;
  179.  
  180.     for (int count = 0; count < 50; count++)
  181.         {
  182.             ins >> lab8[count];
  183.  
  184.         }
  185.  
  186.     for (int count = 0; count < 50; count++)
  187.         {
  188.             if (lab8[count] % 2 == 0)
  189.                 {
  190.                     lab8[count] = 0;
  191.                 }
  192.         }
  193.  
  194.     for (int count = 0; count < 50; count++)
  195.         {
  196.             if (lab8[count] > largest)
  197.                 {
  198.                     largest = lab8[count];
  199.                 }
  200.         }
  201.     ins.close();
  202.     cout << largest << endl;
  203.     */
  204.  
  205.     ///////////////////////////////////////////////////////////////////
  206.     // Assume that your computer has the very limited capability of being able to read and write only single-integer digits
  207.     /*
  208.     #define infile4 "lab8_digits1.txt"
  209.     #define infile5 "lab8_digits2.txt"
  210.     ifstream ins;
  211.    
  212.  
  213.     int number1 [30];
  214.     int number2 [30];
  215.     int result [30];
  216.     int digitsCounted = 0;
  217.     int carryDigit = 0;
  218.  
  219.     ins.open(infile4);
  220.  
  221.     while (!ins.eof())
  222.         {
  223.             ins >> number1[digitsCounted];
  224.             digitsCounted = digitsCounted + 1;
  225.         }
  226.  
  227.     ins.close();
  228.     lab8ArrayFiller (number1, 30, digitsCounted);
  229.     digitsCounted = 0;
  230.  
  231.     ins.open(infile5);
  232.  
  233.     while (!ins.eof())
  234.         {
  235.             ins >> number2[digitsCounted];
  236.             digitsCounted = digitsCounted + 1;
  237.         }
  238.  
  239.     ins.close();
  240.  
  241.     lab8ArrayFiller (number2, 30, digitsCounted);
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.     for (int index = 29; index > -1; index--)
  249.         {
  250.             if ((number1[index] + number2[index] + carryDigit) > 9)
  251.                 {
  252.                    
  253.                     result[index] = (number1[index] + number2[index] + carryDigit) - 10;
  254.                     carryDigit = 1;
  255.                 }
  256.             else
  257.                 {
  258.                     result[index] = number1[index] + number2[index] + carryDigit;
  259.                     carryDigit = 0;
  260.                 }
  261.         }
  262.  
  263.     for (int index = 0; index < 30; index++)
  264.         {
  265.             cout << result[index] << ", ";
  266.         }
  267.  
  268.     */
  269.  
  270.  
  271.     ///////////////////////////////////////////////////////////////////
  272.     //Lab9 Part A: anagrams
  273.     /*
  274.     const int wordLength = 50;
  275.     char word1[wordLength];
  276.     char word2[wordLength];
  277.     int count = 0;
  278.     char ch = ' ';
  279.  
  280.     string line;
  281.  
  282.     cout << "enter two words" << endl;
  283.     cin >> word1;
  284.     cin >> word2;
  285.  
  286.     int length;
  287.  
  288.     length = strlen (word1);
  289.     cout << length;
  290.    
  291.  
  292.     for (int index1 = 0; index1 < wordLength; index1++)
  293.         {
  294.             if (strlen (word1) != strlen (word2))
  295.                 {
  296.                 break;
  297.                 }
  298.  
  299.             for (int index2 = 0; index2 < wordLength; index2++)
  300.                 {
  301.                 if (word1[index1] == word2[index2])
  302.                     {
  303.                     count++;
  304.                     break;
  305.                     }
  306.                 }
  307.         }
  308.  
  309.  
  310.     if (count == wordLength)
  311.         {
  312.             cout << "Those two words are anagrams!" << endl;
  313.         }
  314.     else
  315.         {
  316.             cout << "Negative. No anagrams found" << endl;
  317.         }
  318.     */
  319.  
  320.  
  321.     //sort(lab8, lab8 + 50);
  322.  
  323.     //for (int count = 0; count < 50; count++)
  324.     //  {
  325.     //      cout << count << ". " << lab8[count] << endl;
  326.  
  327.     //  }
  328.  
  329.  
  330.     ///////////////////////////////////////////////////////////////////
  331.     //Lab9 PartB
  332.  
  333.     char wordA [9] = {'m','o','n','t','h'};
  334.     char wordB [9] = {'z','o','n'};
  335.     char wordC [9] = {'s','e','a'};
  336.     char wordD [9] = {'s','e','a','l'};
  337.     bool temp;
  338.  
  339.     //temp = lab9alphabetical (wordA, wordB, 9);
  340.  
  341.     cout << temp;
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.     getch();
  349.  
  350.     return 0;
  351. }
  352.  
  353.  
  354.  
  355. void showValues (int numbs[], int size)
  356.     {
  357.         for (int index= 0; index < size; index++)
  358.             {
  359.                 cout << numbs[index] << " " ;
  360.             }
  361.         numbs[0] = 200;                                                             // array names in functions are like reference variables - changes made to array in a function
  362.         cout << endl << endl <<endl;                                                // are reflected in actual array in calling function.
  363.     }
  364.  
  365.  
  366.  
  367. void lab8ArrayFiller (int numbers[], int size, int count)
  368.     {
  369.     int digitsRemaining = 30;
  370.  
  371.     digitsRemaining = digitsRemaining - count;
  372.  
  373.     while (digitsRemaining != 0)
  374.         {
  375.             for (int index = 28; index > -1; index--)
  376.                 {
  377.                     numbers[index + 1] = numbers[index];
  378.                 }
  379.             numbers[0] = 0;
  380.             digitsRemaining = digitsRemaining - 1;
  381.         }
  382.  
  383.     }
  384.  
  385.  
  386. void lab9sort (char word1[], char word2[], char word3[], char word4[], int)
  387.     {
  388.  
  389.  
  390.  
  391.  
  392.     }
  393.  
  394.  
  395. bool lab9alphabetical (char name1[], char name2[], int size)
  396.     {
  397.         for (int index = 0; index < size; index++)
  398.             {
  399.                  if (name1[index] < name2[index])
  400.                     {
  401.                         return true;
  402.                     }
  403.                 else if (name2[index] < name1[index])
  404.                     {
  405.                         return false;
  406.                     }
  407.             }
  408.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement