Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] C++ program that performs element-wise inverse of a matrix A

c++ element matrix inverse

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 20 November 2012 - 12:03 PM

Input Format
The input starts with two integers M and N, where 0 < M, N = 50 followed by M rows of N integers whose values ranges from 0 to 255. Those numbers comprise the values of the matrix A.

Output Format
The output should be the element-wise inverse matrix C where C equals 255 - A.

Sample Input

2 3

130 45 109
200 255 75

Sample Output

125 210 146
55 0 180

#2 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 20 November 2012 - 02:31 PM

Just traverse the array and assign the value of 255-array[index] to array[index].

We are not going to do your program for you, if you have specific problems with your code, you should post it here and let us know how we could help.

#3 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 21 November 2012 - 06:57 AM

thanks so much. ^^

#4 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 21 November 2012 - 08:26 AM


#include<stdio.h>



int main () {



int m;

int n;

int A[3][2];

int C;





for(m=0;m<2;m++) {

  for(n=0;n<3;n++) {

				  

	 printf("\nInsert value for A[%d][%d]: ", m, n );

	 scanf("%d", &A[m][n]);

	 getchar();





  if (A[m][n]>= 255) {

		 A[m][n] = 255;

	  }

  else if (A[m][n]<=0) {

		A[m][n] = 0;

		}  

			

}	



}



printf("Your Inputed Values:");



for(m=0;m<2;m++) {

  for(n=0;n<3;n++) {



	 printf("  %d  ", A[m][n]);

   }

  }





for(m=0;m<2;m++) {

  for(n=0;n<3;n++) {

				  

  C  = 255 - A[m][n];

   printf("  %d  ", C);



  }

}





return 0;



}



#5 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 21 November 2012 - 12:07 PM

You have it.
Just format is so it uses an endline after each row!

PS: use [ code ] tags

#6 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 21 November 2012 - 12:23 PM

hmm... not really. I think i did something wrong in this part



Read more: http://forum.codecal.../#ixzz2CtFr3cEn

for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {
				  
	 printf("\nInsert value for A[%d][%d]: ", m, n );
	 scanf("%d", &A[m][n]);
	 getchar();


  if (A[m][n]>= 255) {
		 A[m][n] = 255;
	  }
  else if (A[m][n]<=0) {
		A[m][n] = 0;
		}  
			
}	

}

printf("Your Inputed Values:");

for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {

	 printf("  %d  ", A[m][n]);
   }
  }

as i try to run it, it exists after entering the last value.
Like the running window closes in a split second after entering the last value.

[color=#000000][background=rgb(255, 255, 255)]for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {
  
printf("\nInsert value for A[%d][%d]: ", m, n );
scanf("%d", &A[m][n]);
getchar();


  if (A[m][n]>= 255) {
A[m][n] = 255;
  }
  else if (A[m][n]<=0) {
	A[m][n] = 0;
	}  
	
}	

}

printf("Your Inputed Values:");[/background][/color][/left]
[left][color=#000000][background=rgb(255, 255, 255)]

Read more: http://forum.codecal.../#ixzz2CtFjLZnY[/background][/color]


[/left]
[left][color=#000000][background=rgb(255, 255, 255)]for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {
  
printf("\nInsert value for A[%d][%d]: ", m, n );
scanf("%d", &A[m][n]);
getchar();


  if (A[m][n]>= 255) {
A[m][n] = 255;
  }
  else if (A[m][n]<=0) {
	A[m][n] = 0;
	}  
	
}	

}

printf("Your Inputed Values:");
Read more: http://forum.codecal.../#ixzz2CtFjLZnY[/background][/color]



sorry, i posted that part multiple times by mistake. :P

#7 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 21 November 2012 - 02:01 PM

Oh, the closing window problem again..That's not a problem with your code.
Which IDE are you using?

your code works: (almost: logic error at print statement. you can find it :) )
neil@crunchbang:~/ch$ vi matrix.cpp
neil@crunchbang:~/ch$ g++ matrix.cpp
neil@crunchbang:~/ch$ ./a.out

Insert value for A[0][0]: 1

Insert value for A[0][1]: 2

Insert value for A[0][2]: 3

Insert value for A[1][0]: 4

Insert value for A[1][1]: 5

Insert value for A[1][2]: 6
Your Inputed Values: 1 2 4 4 5 6 254 253 251 251 250 249
neil@crunchbang:~/ch$


#8 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 23 November 2012 - 05:33 AM

the dev-C++

ugh... what can i do about it? I can't even see my output, so i can't see the problem in it.

Ah! Got it! though the closing-window problem is still a problem, I was able to see my output for a split second.
Below is the code. Thanks so much!


#include<stdio.h>

int main () {

int m;
int n;
int A[3][2];
int C;


for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {
				  
	 printf("\nInsert value for A[%d][%d]: ", m, n );
	 scanf("%d", &A[m][n]);
	 getchar();


  if (A[m][n]>= 255) {
		 A[m][n] = 255;
	  }
  else if (A[m][n]<=0) {
		A[m][n] = 0;
		}  
			
}	
}

printf("Your Inputed Values:");

for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {

	 printf("  %d  ", A[m][n]);
   }
  }


for(m=0;m<2;m++) {
  for(n=0;n<3;n++) {
				  
   C = A[m][n]/16;
   printf("  %d  ", C);
  }
}


return 0;

}


#9 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts

Posted 23 November 2012 - 07:09 AM

I've never used an IDE apart from CB but, a simple cin>> statement before the end (before return 0 ) would help you see the output.

#10 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 24 November 2012 - 10:21 AM

Run your program from shell (cmd.exe). Also, your assignment says 0 < M, N <= 50 if I read it correctly (not just 3x2 size). Output would look really nice if you could somehow print it like a matrix.

The roots of education are bitter, but the fruit is sweet.


#11 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 24 November 2012 - 04:17 PM

Worry not Abbie, as the guys are telling you, there is no problem in your code.

Thing is the IDE calls a cmd console in order to execute your program, so when your program finishes, it closes and gives back control to the IDE. if you run it from the shell as FD said, you will get to see the display.

in the other hand, is good if you add at the end of your program some final input like: press E to exit, so the program will not end until you press E, so you can watch the outputs all you want. there is also the bad famed System( "pause" ); lol

#12 abbie

abbie

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 24 November 2012 - 05:22 PM

@ Flying Dutchman, thanks. haha. how could i miss that part. :D
@ Black Rabbit, I added the System("pause"); and it worked! Thanks so much guys!




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download