array matrix problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: array matrix problem

  1. #1
    Join Date
    Jul 2013
    Posts
    11

    array matrix problem

    Write your question here.
    hello everyone, ok so i have an array matrix called tmat,,and i know that in every row of tmax there are values which repeat two times...and am writing a code to extract the values WHICH DOES NOT REPEAT into another matrix called tcopy...the codes compiles fine...and it writes nicely to file...but without the desired result...so is surely me ...can some one check it out and tell me where am doing it wrong? thanks
    One last question...how can i get the array tcopy written to file in the form 5x3...and not all the figures in line one after the other? i mean i wish to see the matrix like a matrix on file..not like a list of numbers..thank you again
    Code:
     #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    const int R = 5;
    const int C = 5;
    const int R1 = 5;
    const int C1 = 3;
    
    void extract (int mat [R][C]);
    void filling (int mat [R1][C1], int value);
    
    int control= 0, st_r = 0, st_c = 0, found = 0;
    int rr = 0, cc = 0 ;
    int tcopy [5][3];
    int main (){
        int tmat [5][5] = {{1,2,3,4,4,},
                           {7,4,5,7,6,},
                           {4,0,7,9,0,},
                           {4,5,5,9,0,},
                           {4,5,9,9,0,},
                           };
    
    extract(tmat);
    
    fstream file;
    file.open("oppose.txt", ios::in | ios::out);
    if (file.is_open()){
        for(int k= 0; k< 5; k++)
            for(int i = 0; i <3; i++)
    
            {file<<tcopy[k][i]<<", ";}
            cout<<"copied to file successfully";
        file.close();
    } else cout<<"cound not open file...";
    
    return 0;
    
    
                            }
    
    void extract (int mat [R][C]){
    while (control != R*C)
    {
        int k = mat [st_r][st_c];
    
        for(int i = 0; i<R ; )
        for (int c= 0; c<C; c++)
        {
            { if (k == mat [i][c]); //over here i want to loop over the whole row.
                      found++;}// is it one at a time? or am looping the row?
            if (found < 2) {filling(tcopy,k );}
                found = 0;
                if(st_c < C) st_c++;
                else{ st_c = 0;
                if(st_r < R) st_r ++;} i++;
                }
                control ++;
         }
    }
    
    void filling (int mat [R1][C1], int value)
    {
        mat[rr][cc] = value;
        if (cc < C1) cc++;
        else { cc = 0;
        if(rr<R1) rr++;}
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    hello everyone, ok so i have an array matrix called tmat,,and i know that in every row of tmax there are values which repeat two times...and am writing a code to extract the values WHICH DOES NOT REPEAT into another matrix called tcopy..
    What if there are 4 unique values on the row?
    Code:
     {7,4,5,7,6,},
    I see 7, 4, 5, and 6. So why did you assume that there are only 3 columns in the output array?

    Second, you #include <vector> but you don't use anything related to std::vector.
    .the codes compiles fine
    All "compiling fine" does is prove that the program has no syntax errors. It doesn't prove that the program runs correctly or gives the desired results. So stating that the program compiles fine means basically nothing.
    ..and it writes nicely to file...but without the desired result...
    If it writes nicely but without the desired results, is it writing nicely?
    can some one check it out and tell me where am doing it wrong? thanks
    Do you have access to a debugger? If so, you should be running your program through the debugger and figuring out where it goes wrong. You wrote the program, so where does the program go wrong where you expect it to do something else?
    One last question...how can i get the array tcopy written to file in the form 5x3...
    Only if you can answer the first question -- why is it only 3 columns, when on the second row, there are 4 unique values?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    Hi mr Paul,
    well there are no four unique values on any row...the matrix am working on is a particular one and i just want to extract the non repeating values in tcopy...is not a general thing..but the data am having is a particular data and I KNOW!! that on every row a number repeats itself twice!

    i assume there is gonna be only three columns in tcopy because since i know there is a number repeating itself in tmat...in extracting the non repeating numbers to tcopy i will have only three columns... these are things particular to the work am doing... try to understand..

    about the vector inclusion not used..that doesnt cause no problem...i was thinking i was gonna use a container then i changed my mind...
    and if you look well mr...{7,4,5,7,6} are all not unique...7 repeats it self here...thanks...
    so pls try to give me some help...i dont know how to use a debugger..hell! i dont have one...
    I know you guys are experts...come on..check on the code and help me out...I need to do this thing...

    Quote Originally Posted by Paul McKenzie View Post
    What if there are 4 unique values on the row?
    Code:
     {7,4,5,7,6,},
    I see 7, 4, 5, and 6. So why did you assume that there are only 3 columns in the output array?

    Second, you #include <vector> but you don't use anything related to std::vector.
    All "compiling fine" does is prove that the program has no syntax errors. It doesn't prove that the program runs correctly or gives the desired results. So stating that the program compiles fine means basically nothing.
    If it writes nicely but without the desired results, is it writing nicely?
    Do you have access to a debugger? If so, you should be running your program through the debugger and figuring out where it goes wrong. You wrote the program, so where does the program go wrong where you expect it to do something else?
    Only if you can answer the first question -- why is it only 3 columns, when on the second row, there are 4 unique values?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,389

    Re: array matrix problem

    One last question...how can i get the array tcopy written to file in the form 5x3...
    Try this..
    Code:
    for (int k = 0; k < R1; k++) {
            for (int i = 0; i < C1; i++) {
                  file << tcopy[k][i];
                  if (i != (C1 - 1)) file << ", ";
            }
            file <<endl;
    }
    but bear in mind Paul's comment re size of array.

    You are defining const ints for the array sizes, but you are not using these in your function main! Your tcopy, tmat and array index loops should be defined in terms of your consts.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    Hi mr Paul,
    well there are no four unique values on any row...
    So why did you post such a row? We are looking at what you post, what you described, and working with that information. If you post things that aren't what you're actually dealing with, how are we supposed to know this?
    and if you look well mr...{7,4,5,7,6} are all not unique...7 repeats it self here...thanks...
    The unique values are 7, 4, 5, and 6. I don't know what you mean by "unique", but the C++/algorithm definition of "unique" is exactly as I stated. If I placed those numbers in a container that holds unique values, I will get 7,4.5,6 (maybe in a sorted order, such as std::set would have done).
    i dont know how to use a debugger..hell! i dont have one...
    What compiler are you using? All modern compilers have one.
    I know you guys are experts...come on..check on the code and help me out...I need to do this thing...
    Is this a homework problem? If so, then having experts debug your code is considered cheating. Debugging is part and parcel of learning how to write a program. It isn't just about compiling and running code.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 28th, 2013 at 02:17 PM.

  6. #6
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    is really a pain in the backside when this homework **** comes up...and lets assume is a homework...if one is having a problem on a homework i think the person must ask for help no? IS NOT A HOMEWORK but am a private guy trying to use programming to make work easier and handle lots of data which doing by hand will take me hours and possibly contain errors!!!

    is ok mr. paul...you've not been helpful at all..zero!..thanks..bye...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    is really a pain in the backside when this homework **** comes up...and lets assume is a homework...if one is having a problem on a homework i think the person must ask for help no?
    Not if you refuse to debug the program yourself first. Read the FAQ, especially the second item:

    http://forums.codeguru.com/showthrea...ork-assignment

    If you have debugged your program, then tell us where in the program the line of code, function, etc. diverges from what you expect. Or do you want us to do this work?

    I have taught C++, and if a student ever wrote a program and then asked an expert to debug the code for them while they sit back and wait for the results, that is an instant failure for that student. It doesn't matter how much time they took to write the program, asking someone else to debug without any feedback from the student as to what they have discovered when they debugged the program results in a failure.
    IS NOT A HOMEWORK but am a private guy trying to use programming to make work easier and handle lots of data which doing by hand will take me hours and possibly contain errors!!!
    But that is the nature of programming -- you may have errors. No one writes an error-free program the first time, even experts. That's why debugging is a mandatory skill that has to be learned. Isn't this an excellent opportunity to learn this?

    You say you don't have a debugger. I asked what compiler are you using, so that we can show you how to debug. You know, teach a man to fish instead of giving them fish.
    is ok mr. paul...you've not been helpful at all..zero!..thanks..bye...
    Code:
    #include <vector>
    #include <set>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    template <typename T>
    std::vector<T> CreateUniqueVector(T* firstItem, T* lastItem)
    {
       std::set<T> tSet;
       std::vector<T> vReturn;
       T* curItem = firstItem;
       while ( curItem != lastItem )
       {
           if ( tSet.insert(*curItem).second)
             vReturn.push_back(*curItem);
          ++curItem;
        }
        return vReturn;
    }
    
    using namespace std;
    
    typedef std::vector<int> IntVect;
    typedef std::vector<IntVect> IntVect2D;
    //...
    int main()
    {
     int tmat [5][5] = {{1,2,3,4,4,},
                              {7,4,5,7,6,},
                              {4,0,7,9,0,},
                              {4,5,5,9,0,},
                              {4,5,9,9,0,},
                           };
       IntVect2D tCopy;
       for (int i = 0; i < 5; ++i )
          tCopy.push_back(CreateUniqueVector<int>(&tmat[i][0], &tmat[i][5]));
          
       for (int i = 0; i < 5; ++i)      
       {
          std::copy(tCopy[i].begin(), tCopy[i].end(), std::ostream_iterator<int>(cout," "));
          cout << "\n";
       }      
    }
    If this isn't homework as you claim, then here is a solution. By using container classes (I use the std::set for uniqueness testing), then the problem becomes a matter of learning how to use the container classes properly.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 28th, 2013 at 03:08 PM.

  8. #8
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    OK thank you Sir Mckenzie..i use codeblocks and sometimes devc++(they come with compilers i actually dont know the name..i think i read gcc somewhere but am not sure) but mostly codeblocks...your solutions method looks like mmm...I mean...can't the thing be resolved using simple basic c++ code like i did...normal looping over arrays...can't it be done like that? can't just a lil correction to what i wrote be done to make it work? am going to try to learn how to debug this very night!!!

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    OK thank you Sir Mckenzie..i use codeblocks and sometimes devc++(they come with compilers i actually dont know the name..i think i read gcc somewhere but am not sure) but mostly codeblocks...
    Then the debugger (at least the base debugger) is gdb. There are GUI's built on top of gdb to make it easier to use, but that's what you have with gcc/Codeblocks. I'm sure the CodeBlocks IDE has a debug menu item which is the gateway to the gdb debugger.
    your solutions method looks like mmm...I mean...can't the thing be resolved using simple basic c++ code like i did...
    That's the thing. Your code is not simple in the sense that even I have to look at it 20 ways to ensure it works. Using containers and algorithms, anyone can look at the code I wrote and tell you right away it works.

    The code I wrote looks complicated, but it isn't. I wrote it once, and it worked the first time I compiled and ran it. The reason why I was confident it would work is because I used C++ in a way where I'm not figuring out loops, using booleans all over the place, if() logic etc, as your code is doing.
    normal looping over arrays
    The code I wrote has a loop.
    ...can't it be done like that? can't just a lil correction to what i wrote be done to make it work?
    Again, that's the rub. Maybe it is easy to fix, or maybe the fix is to use more booleans, or to rewrite the whole loop over again. And even if that were done, is it guaranteed to work for all cases? And on top of that, I would have to debug (there is that word again) to ensure what we have written will work. So it winds up that we are doing the work you should have been doing all along to make your version run correctly, and that is to fire up the debugger, single step through the code, etc.

    But to give you insight on what I wrote, let's go over the function:
    Code:
    template <typename T>
    I made it a template, so it works for any type. What if next week, it is double instead of ints, or strings instead of ints? This covers basically those cases. The "T" is the type that is stored in the array.
    Code:
    std::vector<T> CreateUniqueVector(T* firstItem, T* lastItem)
    This function returns a vector of unique items. The arguments are pointers to the first item and last items in the sequence of values you want to have unique.
    Code:
       std::set<T> tSet;
    A set is a container that holds only unique items, so I used it to my advantage here.
    Code:
       std::vector<T> vReturn;
    This is my vector of unique values that I will return when done.
    Code:
       T* curItem = firstItem;
       while ( curItem != lastItem )
    I will start with the first item, and loop until the last item is done
    Code:
           if ( tSet.insert(*curItem).second)
             vReturn.push_back(*curItem);
    I will insert the item in my array into the set. When you insert an item into a set, a value is returned telling you whether the new item was inserted or not. This value is returned as a "std::pair", where the "second" is true if the item is inserted, false otherwise. So if the item was inserted into the set, I know it was unique (since again, sets do not hold duplicates). I then just push_back this value into my return vector.
    Code:
          ++curItem;
    Look at the next item.
    Code:
        return vReturn;
    When done, return the resulting vector.

    Now look how I call the function in the main() program:
    Code:
     for (int i = 0; i < 5; ++i )
          tCopy.push_back(CreateUniqueVector<int>(&tmat[i][0], &tmat[i][5]));
    I loop over each row, passing the first and last element of each row to the CreateUniqueVector function. Then the return value of that function is pushed onto the 2D vector, thus building up the 2d vector of unique arrays.

    The outputting is nothing more than a shortcut using std::copy and iterators. You could have just written a simple loop calling cout.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 28th, 2013 at 03:43 PM.

  10. #10
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    ok great...thanks very much Sir Mckenzie...I use this book called jumping into c++..alex allain..and it teaches debugging with codeblocks...first thing it ask to do is..go to Project/ build options..select debug...then select ..produce debugging symbols[-g]
    but am having build options in project all grayed out so i cant select it and continue with the tutorials...do you know how to fix it? can you give some advice? or maybe you advice a better IDE to use?...
    Thanks

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,389

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    OK thank you Sir Mckenzie..i use codeblocks and sometimes devc++(they come with compilers i actually dont know the name..i think i read gcc somewhere but am not sure) but mostly codeblocks...your solutions method looks like mmm...I mean...can't the thing be resolved using simple basic c++ code like i did...normal looping over arrays...can't it be done like that? can't just a lil correction to what i wrote be done to make it work? am going to try to learn how to debug this very night!!!
    The code that Paul kindly produced is simple, normal c++ code! This is how good c++ programs are written. Looking at your code, with the exception of using the c++ i/o streams rather than the c standard library forms, this could have been c code. Using the facilities of the STL makes c++ much more powerful and easier to write required functanality that works first time it compiles. If you find this sort of code alien you are not getting the best out of c++. You can write c++ code that looks like c code but as you're found it's much harder to get it right first time and time spent trying to debug 'normal looping...' is time wasted that could be spent on writing another program. I would urge you to try to get familiar with the functanality provided by the STL and use it where appropriate.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: array matrix problem

    FWIW, I think when he says "unique", he means numbers that only occur once, so using a set to ensure unique numbers isn't going to help him.
    Last edited by GCDEF; July 28th, 2013 at 05:22 PM.

  13. #13
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    that is what i meant GCDEF...i was extracting numbers that occured only once in the row... anyway thanks very much 2kaud..am gonna familiarize my self very well with the STL...so GCDEF...you think you have a better solution? or advice? any contribution is welcomed...thanks

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: array matrix problem

    Quote Originally Posted by TheLionKing View Post
    that is what i meant GCDEF...i was extracting numbers that occured only once in the row... anyway thanks very much 2kaud..am gonna familiarize my self very well with the STL...so GCDEF...you think you have a better solution? or advice? any contribution is welcomed...thanks
    There are a lot of things you could do. If you're only looking at single digits, you could create an int array of size 10 and initialize each element to 0. For each digit you encounter in your list of numbers, increment the array element at the index position of the encountered number. When you're done, iterate the array and output each index whose element has a value of 1.

  15. #15
    Join Date
    Jul 2013
    Posts
    11

    Re: array matrix problem

    Am sorry guys but maybe i didnt make my self clear...because after trying to use the code of mckenzie and not getting the desired result plus GCDEF's comment make me understand that we're not understanding each other...so what i want to do is this...i have tmat int tmat [5][5] = {{1,2,3,4,4,},
    {7,4,5,7,6,},
    {4,0,7,9,0,},
    {4,5,5,9,0,},
    {4,5,9,9,0,},
    };
    after program run i should have tcopy like this
    {1 2, 3}
    {4,5,6}
    {4,7,9}
    {4,9,0}
    {4,5,0} with only non repeating numbers in tmat now in tcopy...
    I still feel my code should be able to do it somehow..after some lil corrections for the mean time that am not so used to the STL... but am learning...thanks

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center