Advertisement
maramizo

assignment

Oct 17th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. // maxValplusCertainNum.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. int main()
  9. {
  10.     int narray[3][3], choice, sChoice;
  11.    
  12.     std::cout << "Please input 9 numbers: " << std::endl;
  13.  
  14.     for (int i = 0; i < 3; i++)
  15.         for (int j = 0; j < 3; j++)
  16.             std::cin >> narray[i][j];
  17.  
  18.     std::cout << "Current array: " << std::endl;
  19.     std::cout << "\tColumn 1:\tColumn 2:\tColumn 3:" << std::endl;
  20.     for (int i = 0; i < 3; i++)
  21.     {
  22.         std::cout << "Row " << i+1 << ": \t";
  23.         for (int j = 0; j < 3; j++)
  24.         {
  25.             std::cout << narray[i][j];
  26.             std::cout << "\t\t";
  27.         }
  28.         std::cout << std::endl;
  29.     }
  30.     std::cout << "1. Max value  2. Certain Number Search" << std::endl;
  31.     std::cout << "Please a number as input to your choice: ";
  32.  
  33.     std::cin >> choice;
  34.     std::cout << std::endl;
  35.  
  36.     switch (choice)
  37.     {
  38.         case 1:
  39.         {
  40.             int max;
  41.             std::cout << "Please input the number of the row you're searching in, or enter 4 for a diagonal search, or 0 for a search across all the array: ";
  42.             std::cin >> sChoice;
  43.             std::cout << std::endl;
  44.             if (!sChoice)
  45.             {
  46.                 max = narray[0][0];
  47.                 for (int i = 0; i < 3; i++)
  48.                     for (int j = 0; j < 3; j++)
  49.                         max < narray[i][j] ? max = narray[i][j] : 0;
  50.  
  51.                 std::cout << "Maximum number across the array is " << max << "." << std::endl;
  52.                 break;
  53.             }
  54.             if (sChoice < 4 && sChoice > 0)
  55.             {
  56.                 max = narray[sChoice-1][0]; //Row search, for Column search: max = narray[0][sChoice];
  57.                
  58.                 for (int i = 0; i < 3; i++)
  59.                     max < narray[sChoice-1][i] ? max = narray[sChoice-1][i] : 0;
  60.  
  61.                 std::cout << "Maximum number in row number " << sChoice << " is " << max << "." << std::endl;
  62.                 break;
  63.             }
  64.             if (sChoice == 4)
  65.             {
  66.                 max = narray[0][0];
  67.                 for (int i = 1; i < 3; i++)
  68.                     max < narray[i][i] ? max = narray[i][i] : 0;
  69.                 std::cout << "The largest number across the left to right diagonal is " << max << "." << std::endl;
  70.             }
  71.             break;
  72.         }
  73.         case 2:
  74.         {
  75.             std::cout << "Please input the number you're searching for: ";
  76.             std::cin >> sChoice;
  77.             std::cout << std::endl;
  78.            
  79.             int row = -1, column;
  80.             for (int i = 0; i < 3; i++)
  81.             {
  82.                 for (int j = 0; j < 3; j++)
  83.                 {
  84.                     if (sChoice == narray[i][j])
  85.                     {
  86.                         row = i;
  87.                         column = j;
  88.                         break;
  89.                     }
  90.                 }
  91.             }
  92.             if (row == -1)
  93.                 std::cout << "The number " << sChoice << " does not exist in the array." << std::endl;
  94.             else
  95.                 std::cout << "The number " << sChoice << " exists in row number " << row+1 << " and column number " << column+1 << "." << std::endl;
  96.             break;
  97.         }
  98.         default:
  99.         {
  100.             while (choice != 1 && choice != 2)
  101.             {
  102.                 std::cout << "Please input 1 or 2 only: ";
  103.                 std::cin >> choice;
  104.                 std::cout << std::endl;
  105.             }
  106.             break;
  107.         }
  108.     }
  109.     system("PAUSE");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement