Thread: finding an int in an array

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    finding an int in an array

    im going to take this summer course for advanced science/math/a little programming over the summer (its at OSU, i believe its called SI2003, its for current high school freshmen and sophmores) and you have to answer some sample questions to see wut you know
    one of the questions is you have to make a 5 by 7 array that will hold 35 single diget ints the person types in. then it tells how many 0s there are, 1s there are etc.
    i can do all of that, but then you have to make it tell where they are (ie [3][5] or whatever) i tried several methads but none of them worked. i dont want someone to tell me the answer cuz im supposed to do it myself, but if you could point me in the right direction, it would be appreciated.
    here is my latest attempt:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int zero, one, two, three, four, five, six, seven, eight, nine;
    int osu[7][5];
    int zeroa[5][7];
    int zerob[5][7];
    int onea[5][7];
    int oneb[5][7];
    int twoa[5][7];
    int twob[5][7];
    int threea[5][7];
    int threeb[5][7];
    int foura[5][7];
    int fourb[5][7];
    int fivea[5][7];
    int fiveb[5][7];
    int sixa[5][7];
    int sixb[5][7];
    int sevena[5][7];
    int sevenb[5][7];
    int eighta[5][7];
    int eightb[5][7];
    int ninea[5][7];
    int nineb[5][7];
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, x;
    
    void input()
    {
      cout <<"Please enter 35 single diget integers.\n";
      for(a = 0;a < 7;a++)
      {
        for(b = 0;b < 5;b++)
        {
          cin >>x;
          if(x<0 || x>9);
          {
            cout <<"The number you have entered is in valid. Please try again.\n";
          }
          else
          {
            osu[a][b] = x;
          }
        }
      }
    }
    
    void count()
    {
      for(a = 0;a < 7;a++)
      {
        for(b = 0;b < 5;b++)
        {
          if(osu[a][b] == 0)
          {
            zero++;
            zeroa[c] = c;
            zerob[d] = d;
            c++;
            d++;
          }
        else{
          if(osu[a][b] == 1)
          {
            one++;
            onea[e] = e;
            oneb[f] = f;
            e++;
            f++;
          }
        }
        else{
          if(osu[a][b] == 2)
          {
            two++;
            twoa[g] = g;
            twob[h] = h;
            g++;
            h++;
          }
        }
        else{
          if(osu[a][b] == 3)
          {
            three++;
            threea[i] = i;
            threeb[j] = j;
            i++;
            j++;
          }
        }
        else{
          if(osu[a][b] == 4)
          {
            four++;
            foura[k] = k;
            fourb[l] = l;
            k++;
            l++;
          }
        }
        else{
          if(osu[a][b] == 5)
          {
            five++;
            fivea[m] = m;
            fiveb[n] = n;
            m++;
            n++;
          }
        }
        else{
          if(osu[a][b] == 6)
          {
            six++;
            sixa[o] = o;
            sixb[p] = p;
            o++;
            p++;
          }
        }
        else{
          if(osu[a][b] == 7)
          {
            seven++;
            sevena[q] = q;
            sevenb[r] = r;
            q++;
            r++;
          }
        }
        else{
          if(osu[a][b] == 8)
          {
            eight++;
            eighta[s] = s;
            eightb[t] = t;
            s++;
            t++;
          }
        }
        else{
          if(osu[a][b] == 9)
          {
            nine++;
            ninea[u] = u;
            nineb[v] = v;
            u++;
            v++;
            }
          }
        }
      }
    }
    
    void output()
    {
      cout <<"There were a total of " <<zero <<" zeros.\n"
      /*here i would put something like 
      cout <<"They were at location:" <<zero[a][b];
      or something like that, but im not sure */
      cout <<"There were a total of " <<one <<" ones.\n"
      cout <<"There were a total of " <<two <<" twos.\n"
      cout <<"There were a total of " <<three <<" threes.\n"
      cout <<"There were a total of " <<four <<" fours.\n"
      cout <<"There were a total of " <<five <<" fives.\n"
      cout <<"There were a total of " <<six <<" sixs.\n"
      cout <<"There were a total of " <<seven <<" sevens.\n"
      cout <<"There were a total of " <<eight <<" eights.\n"
      cout <<"There were a total of " <<nine <<" nines.\n"
    }
    
    int main()
    {
      input();
      output();
      system("Pause");
      return 0;
    }
    in addtion to the 50 billion different errors i get, i dont think it would work
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off, you declare all of your index holder arrays as [5][7] but then when you try to enter items into them, you treat them as one-dimensional arrays:
    Code:
    zeroa[c] = c;  // error!  zeroa is a two-dimensional array, not one!
    Declare them as a single size array of size 35:
    Code:
    int zeroa[35];
    Second, the numbers you put into the index arrays is the amount of that number, NOT the index! So zeroa[1][1]=1, zerob[2][2]=2, etc.
    Change it to this:
    Code:
    zeroa[c] = a;
    zerob[d] = b;
    Last, you don't need the counters c-x. Just use the counter you already have for each number.
    Code:
    zero++;
    zeroa[zerro] = a;
    zerob[zerro] = b;
    That'll save you a lot of variables!

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Consider three nested loops like this
    Code:
        for(int digit = 0; digit<=9;++digit) {
    //      stuff
            for(int x = 0;x<5;++x) {
                for(int y = 0;y<7;++y) {
                     if(matrix[x][y] == digit) {
    //               stuff
                     }                  
                }
            }
    //      stuff
        }
    There is no need to figure everything out, then store it in variables then print the variables, just print things as you know them.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A general suggestion....

    Since you have 'till summer, you might pick-up a copy of "Teach Yourself C++ In 21 Days" by Jesse Liberty. (You probably won't actually make it through the book in 21 days, unless you don't have any other homework.) That should get you up-to-speed for the course.

    I really liked the style of the book. It's designed for self-study with questions and exercises at the end of each chapter. With the answers and solutions in the back.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM