EOD: #2

Posts 114 of 14 · Page 1 of 1
EOD: #2
E.O.D.
Error of the Day

Answer to EOD#1: HD got it right. the "++" increment operator was on the wrong side causing the variable to only increment after it was already passed!




E.O.D. #2
This one is tricky! I couldn't figure it out, but hey I rushed cuz I was busy today D: 6 classes! Anyway hope you have better luck then me

What Does it do?
The point of this code is to multiply together to different matrices and save the product in the result matrix and print it in the console window. But of course as you know it doesn't work correctly. However it still compiles and runs and prints data So what's the problem? that's for you to figure out. >:P


Code:
#include <stdio.h>
#include <iostream>

static void matrix_multiply(
                            int result[3][3], /*The result*/
                            int matrix1[3][3], /*One multiplicand*/
                            int matrix2 [3][3] /*The other multiplicand*/
                            )
{
       /* Index into elements of the elements of the matrix*/
       int row, col, element;
       
       for(row = 0; row <3; ++row)
       {
               for(col = 0; col < 3; ++col)
               {
                       result[row][col] = 0;
                       for(element = 0; element < 3; ++element)
                       {
                                   result[row][col] +=
                                                    matrix1[row][element] *
                                                    matrix2[element][col];
                       }
               }
       }
}
/*******************************************************
* matrix_print -- Output the matrix                    *
*******************************************************/
static void matrix_print(int matrix[3][3] /* The matrix to print*/ ) 
{
       int row, col; /*Index into matrix*/
       
       for(row=0; row < 3; ++row)
       {
                  for(col = 0; col <3; ++col)
                  {
                          printf("%o\t", matrix[row][col]);
                  }
                  printf("\n");
       }
}

int main(void)
{
    /* One matrix for multiplication */
    int matrix_a[3][3] = {
        {45,82,26},
        {32,11,13},
        {89,81,25}
    };
    
    /* Another matrix for multiplication */
    int matrix_b[3][3] = {
        {32,43,50},
        {33,40,52},
        {20,12,32}
    };
    /* Place to put result*/
    int result[3][3];
    
    matrix_multiply(result, matrix_a, matrix_b);
    matrix_print(result);
    system("pause");
    return(0);
    
}
Here is what it outputs. Is this output correct?


Ok. This one is very pretty difficult so I will be including some hints. If you don't want any hints don't look down and turn around and you might want to zip up your fly while your at it. The Hints are written in white so you have to highlight them to see















-----------------------------------------HINTS---------------------------------------


HINT#1:Hmmm... have you looked at the output? There's no 8's or 9's in it?


HINT#2: Have you tried running the program with other numbers still the same effect?


HINT#3:
Do you know your escape character sequences?

HINT#4:
This site might help: http://ecet.calumet.purdue.edu/~ofar...ent/printf.pdf

LAST HINT!:
octal? decimal?

-----------------------------No More Hints Sorry----------------------------

Post what you think it is. Answer will be revealed tomorrow in the final EOD for this week.
Interesting jump in difficulty.

Infact that wasn't fair because isn't this for C++ no C >_<. I actually had to look at a hint for that one. I only did C shortly months ago.


Eitherway, i kind of restructured how it multiplied because it pissed me off, but the main problem is the octal thingy (%o). I replaced with the %d thingy. Oh also the result[x][x] + = stuffs. I replaced that just with =, code below.
[php]
#include <stdio.h>
#include <iostream>

static void matrix_multiply(
int result[3][3], /*The result*/
int matrix1[3][3], /*One multiplicand*/
int matrix2 [3][3] /*The other multiplicand*/
)
{
/* Index into elements of the elements of the matrix*/
int row, col, element;

for(row = 0; row <3; ++row)
{
for(col = 0; col < 3; ++col)
{
/*result[row][col] = 0;
for(element = 0; element < 3; ++element)
{*/
result[row][col] =
matrix1[row][col] *
matrix2[row][col];
//}
}
}
}
/************************************************** *****
* matrix_print -- Output the matrix *
************************************************** *****/
static void matrix_print(int matrix[3][3] /* The matrix to print*/ )
{
int row, col; /*Index into matrix*/

for(row=0; row < 3; ++row)
{
for(col = 0; col <3; ++col)
{
printf("%d\t", matrix[row][col]);
}
printf("\n");
}
}

int main(void)
{
/* One matrix for multiplication */
int matrix_a[3][3] = {
{45,82,26},
{32,11,13},
{89,81,25}
};

/* Another matrix for multiplication */
int matrix_b[3][3] = {
{32,43,50},
{33,40,52},
{20,12,32}
};
/* Place to put result*/
int result[3][3];

matrix_multiply(result, matrix_a, matrix_b);
matrix_print(result);
system("pause");
return(0);
}
[/php]

I am probably wrong though.

Oh hey, i just realised you had a link telling us about printf....... Hehehe, ok maybe it was a bit more fair
Debatable 'bug' since perhaps the goal was to spit out the data in an octal format (not that I for life of me can remember ANY time I ever had to do that..) making another less obvious 'fix' adding a line stating that the output is octal hahaha
Quote Originally Posted by B1ackAnge1 View Post
Debatable 'bug' since perhaps the goal was to spit out the data in an octal format (not that I for life of me can remember ANY time I ever had to do that..) making another less obvious 'fix' adding a line stating that the output is octal hahaha
Yeah :/ i suppose that is a train of thought. Does that mean it is supposed to be result += multiplicants?

not

result = multiplicants?
Nah "result =" is the correct call
And i'm just playing devil's advocate by being a smartass by what I said

So far these problems seem fun & easily solvable without even firing up a compiler etc to get the thing running to see what's going on. I should post up some of the interview C++ questions I used to ask people.
Quote Originally Posted by B1ackAnge1 View Post
Nah "result =" is the correct call
And i'm just playing devil's advocate by being a smartass by what I said

So far these problems seem fun & easily solvable without even firing up a compiler etc to get the thing running to see what's going on. I should post up some of the interview C++ questions I used to ask people.
Yeah i know your kidding around xD. But as i said, this would have taken me far less time if it was straight C++ (by that i mean no C relics) I don't mind C syntax at all, it's just that I only have a pretty basic understanding of the C stuff like printf. I would have totally not understood if i didn't play with C for a week like a couple months before i started C++.

Then again this is pretty interesting. Maybe through these tuts i will get a bit more understand of C++.

Also it is interesting how little BlackAngel says, yet his words are usually the most useful =). I'm glad someone like you is here. BTW, interview C++ questions? That sounds really cool. =D i wanna see how badly i fail. It'll give me more motivation to get better.
Let me find them.. have around my desk here somewhere...Maybe i'll have a IQOD post everyday (Interview Question of the Day) next to Why's Error of the Day posts
Quote Originally Posted by B1ackAnge1 View Post
Let me find them.. have around my desk here somewhere...Maybe i'll have a IQOD post everyday (Interview Question of the Day) next to Why's Error of the Day posts
That would be awesomely amazing =D
Quote Originally Posted by zeco View Post
Interesting jump in difficulty.

Infact that wasn't fair because isn't this for C++ no C >_<. I actually had to look at a hint for that one. I only did C shortly months ago.


Eitherway, i kind of restructured how it multiplied because it pissed me off, but the main problem is the octal thingy (%o). I replaced with the %d thingy. Oh also the result[x][x] + = stuffs. I replaced that just with =, code below.

I am probably wrong though.

Oh hey, i just realised you had a link telling us about printf....... Hehehe, ok maybe it was a bit more fair
I would just like to say I had an equally bad time with this problem, but I was sort of busy today so I didnt have the time to go over 3 or more choices like I did with the last one :P I thought this one was interesting because it had some escape characters I didn't see in my book. The only bad thing about it is I posted it so late HD won't be able to see it to the morning cuz of his timezone. Oh well no cookies for you today HD :P.

and zeco I'm pretty sure you got it right, but I think the += should still be in there. Well nvm I suppose a lot of it is debatable like BA said. Hopefully the next one won't be so... what's the word? When your not sure if something is one thing or another? Oh yeah! ambigous. The next one won't be so ambiguous.
Quote Originally Posted by why06 View Post
The only bad thing about it is I posted it so late HD won't be able to see it to the morning cuz of his timezone. Oh well no cookies for you today HD :P.
You forget I posses the powers of D3D and therefor have a lot of matrix functions to do the work for me :P

I shall post my code shortly

edit:
thought using d3d would be cheating so i just fixed the += and changed %o to %d since I like decimal better
and added a sexy looking skull
Code:
/****************************************************************************************************
                                ...---- ....
                          ..-:"''          ''"-..
                       .-'                        '-.
                     .'               .      .       '.
                  .'    .          .    .       .    .''.
                .'   .    .       .   .    .     .   . ..:.
              .'  .   . .  .       .    .   ..  .   . .... ::.
             ..   .    .      .  .    .      .  ..  . ....: IA.
            .:   .   .    .    .  .   .    .. .  .. .. . ...:IA.
           .:  .   .   ..   .    .      . . .. . ... .. ..:.:VHA.
           ' ..  .  .. .   .        .  . .. . .. . ... ..:.::IHHB.
          .: . .  . .  . .   .  .   . . . ...:.: ... .......:HIHMM.
         .: .... .   . ."::"' .. .   .  . .:.:.: II;,. .. ..:IHIMMA
         ':.:..  ..: :IHHHHHI:: . . .  ...:.:::: .,,,. . ....VIMMHM
        .::: I. .AHHHHHHHHHHAI:: . .:...,: IIHHHHHHMMMHHL: . . VMMMM
       .:.:V.: IVHHHHHHHMHMHHH::..: " .: HIHHHHHHHHHHHHHMHHA.  .VMMM.
       :..V.: IVHHHHHMMHHHHHHHB...  . .: VPHHMHHHMMHHHHHHHHHA I.:VMMI
       ::V..: VIHHHHHHMMMHHHHHH. .    .I": IIMHHMMHHHHHHHHHHHAP I:WMM
       ::". .: .HHHHHHHHMMHHHHHI.   . .:..I: MHMMHHHHHHHHHMHV:': H:WM
       :: . :.:: IIHHHHHHMMHHHHV  .AB A.:.: IMHMHMMMHMHHHHV:' . .IHWW
       '.  ..:..:.: IHHHHHMMHV"  .AVMHMA.:.' VHMMMMHHHHHV:' .  : IHWV
        :.  .:...:" .:.:TPP"    .AVMMHMMA.:. " VMMHHHP.:... .. : IVAI
       .:.   '... .: "'    .   ..HMMMHMMMA:: . ."VHHI:::....  .: IHW'
       ...  .  . ..: IIPPIH:  ..HMMMI.MMMV:I: .  .:ILLH:.. ...:I: IM
     : .   .'"' .: .V". .. .  :HMMM: IMMMI::I. ..: HHIIPPHI::'.P:HM.
     : .  .  .  .. ..: .. .    : AMMM IMMMM..:...:IV" :T::I::.".:IHIMA
     'V: .. .. . .. .  .  .    'VMMV..VMMV :....: V:.:..:....::IHHHMH
       "IHH:.II: .. .:. .  . . . " : HB"" . . ..PI:.::.:: :..:IHHMMV"
        :IP""HHII: .  .  .    . . .'V: . . . ..:IH:.:.:: IHIHHMMMMM"
        :V:. VIMA: I..  .     .  . .. .  .  .:.I:I:..: IHHHHMMHHMMM
        :"VI:.VWMA:: . .:      .   .. .: . ..:.I::.: IVHHHMMMHMMMMI
        :."VIIHHMMA: .  .   .   .:  .: .. . .:.II:I: AMMMMMMHMMMMMI
        : ..VIHIHMMMI...::.,: .,:!"I:!"I!"I!"V:AI: VAMMMMMMHMMMMMM'
        ':.: HIHIMHHA:"!!"I.: AXXXVVXXXXXXXA:." HPHIMMMMHHMHMMMMMV
          V:H:I:MA: W'I :AXXXIXII: IIIISSSSSSXXA.I.VMMM HMHMMMMMM
            'I:: IVA ASSSSXSSSSBBSBMB SSSSSSBBMMMBS.VVMMHI MM'"'
             I::  VPAIMSSSSSSSSSBSSSM MBSSSBBMMMMXXI: MMHIMMI
            .I::. " H: XIIXBBMMMMMMMMMMMMMM MMMBXIXXMMPHIIMM'
            :::I.  ' : XSSXXIIIIXSSBMBSSXXX IIIXXSMMAMI:.IMM
            :::I: .  .VSSSSSISISISSSBI I:ISSSSBMMB:MI:..: MM
            ::.I: .  ':" SSSSSSSISISSXIIXSSSS BMMB:AHI:..MMM.
            ::.I: . . ..:" BBSSSSSSSSSSSSBBBMMM B:AHHI::.HMMI
            :..:: .  . ..::": BBBBBSSBBBMMMB: MMMMHHII::IHHMI
            ':.I: ... ....: IHHHHHMMMMMMMMMMMMMM MHHIIIIHMMV"
              "V: . ..:...: .IHHHMMMMMMMMMMMMMMM MHHHMHHMHP'
                ': . .:::.:.::III:: IHHHHMMMMMHMHMMHHHHM "
                 ":: ....::.:::..:..:: IIIIIHHHHMMMHHMV"
                   " ::.::.. .. .  ...::: IIHHMMMMHMV"
                      "V::... . .I:: IHHMMV"'
                        '"VHVHHHAHHHHMMV: "'
****************************************************************************************************/
#include <stdio.h>
#include <iostream>

static void matrix_multiply(
                            int result[3][3], /*The result*/
                            int matrix1[3][3], /*One multiplicand*/
                            int matrix2[3][3] /*The other multiplicand*/
                            )
{
       /* Index into elements of the elements of the matrix*/
       int row, col, element;
       
       for(row = 0; row <3; ++row)
       {
               for(col = 0; col < 3; ++col)
               {
                       result[row][col] = 0;
                       for(element = 0; element < 3; ++element)
                       {
                                   result[row][col] =
                                                    matrix1[row][element] *
                                                    matrix2[element][col];
                       }
               }
       }
}
/*******************************************************
* matrix_print -- Output the matrix                    *
*******************************************************/
static void matrix_print(int matrix[3][3] /* The matrix to print*/ ) 
{
       int row, col; /*Index into matrix*/
       
       for(row=0; row < 3; ++row)
       {
                  for(col = 0; col <3; ++col)
                  {
                          printf("%d\t", matrix[row][col]);
                  }
                  printf("\n");
       }
}

int main(void)
{
    /* One matrix for multiplication */
    int matrix_a[3][3] = {
        {45,82,26},
        {32,11,13},
        {89,81,25}
    };
    
    /* Another matrix for multiplication */
    int matrix_b[3][3] = {
        {32,43,50},
        {33,40,52},
        {20,12,32}
    };
    /* Place to put result*/
    int result[3][3];
    
    matrix_multiply(result, matrix_a, matrix_b);
    matrix_print(result);
    system("pause");
    return(0);
    
}
my output:
Code:
520    312    832
260    156    416
500    300    800
Very nice. I thought the octal output would stump you for sure even if you did figure out the += thing. Well I guess everyone gets cookies this time. And I'm trying to decide if HD should get bonus points for the giant skull? It's rather intimidating o_O!
Quote Originally Posted by why06 View Post
Very nice. I thought the octal output would stump you for sure even if you did figure out the += thing. Well I guess everyone gets cookies this time. And I'm trying to decide if HD should get bonus points for the giant skull? It's rather intimidating o_O!
i think noone should have hints to confuse everyone even more write your post backwards! that way noone but myself will win and therefor recieve all cookies! RAWR :P
Where's our EOD #3? lol
Quote Originally Posted by B1ackAnge1 View Post
Where's our EOD #3? lol
EOD:#3 is feeling a little under the weather. And so he won't be coming out till tomorrow o_O.
Posts 114 of 14 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

Need help?