Reputation: 1
So i wrote a code to calculate the multiplication of two matrixes. The programm is doing its job by producing the correct outcome. I provide most of the output fuction below:
for (i = 0; i < row; ++i)
{
for (j = 0; j < column; ++j)
{
printf("%d ", result[i][j]);
if (j == column -1)
printf("\n");
}
}
The last two lines help with the correct pressentation of the result, e.g.:
xxx xx
x -xx
x x
where as x stands for an integer. How am I gonna make the programm end without a new line? e.g.:
xxx xx
x -xx
x xPress any key to continue...
instead of what i am getting now:
xxx xx
x -xx
x x
Press any key to continue...
Thanks in advance.
Upvotes: 0
Views: 56
Reputation: 1
The asnwer was trivial, the point is to present the output in an acceptable way for a testing machine, meaning there had to be no space after the end of every new line in order for the test to be passed. I had some troube understanding the logic behind it but eventually after some searching, some help from above and a day of trial and error i figured it out.
for (i = 0; i < row; ++i)
{
for (j = 0; j < column; ++j)
{
printf(j == column -1 ? "%d" : "%d ", result[i][j]);
if (i != row -1 && j == column -1)
printf("\n");
}
}
Upvotes: 0
Reputation: 12708
If you want to end your output without an ending \n
then don't print it at the end of each line. You can print it at the beginning (well, this will make your output with one empty line always at the beginning) or better, Between the lines. E.G:
char *sep = "";
while(whatever_you_like_to_test_to_continue_the_test) {
/* the separator has been initialized to the empty string, so it will
* print nothing the first time. */
printf("%s", sep);
/* now we change it, to whatever we like to use */
sep = "\n";
/* print the body of what you want to print */
printf("....", ...);
}
/* when you reach here, the last part you wrote was the body, not the separator */
For example, in your sample code, you can decorate the elements of your matrix with a pair of brackets for each row, a return line and put everything in another pair of brackets, like this:
#include <stdio.h>
int main() {
int rows = 10, i, cols = 6, j, number = 0;
char *sep1 = "{";
for (i = 0; i < rows; ++i) {
printf("%s", sep1);
sep1 = ",\n ";
char *sep2 = "{";
for (j = 0; j < cols; ++j) {
printf("%s", sep2);
sep2 = ",";
printf("%3d", number++);
}
/* this ends the row */
printf("}");
}
printf("}"); /* the last bracket, and no newline */
} /* main */
will produce:
$ ./a.out
{{ 0, 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10, 11},
{ 12, 13, 14, 15, 16, 17},
{ 18, 19, 20, 21, 22, 23},
{ 24, 25, 26, 27, 28, 29},
{ 30, 31, 32, 33, 34, 35},
{ 36, 37, 38, 39, 40, 41},
{ 42, 43, 44, 45, 46, 47},
{ 48, 49, 50, 51, 52, 53},
{ 54, 55, 56, 57, 58, 59}}$ _
Upvotes: 0
Reputation: 109613
for (i = 0; i < row; ++i)
{
if (i != 0)
printf("\n");
for (j = 0; j < column; ++j)
{
if (j != 0)
printf(" ");
printf("%d", result[i][j]);
}
}
Doing some things outside the loop, before instead of after.
Upvotes: 0
Reputation: 75062
To end without newline, stop printing newline at the end.
for (i = 0; i < row; ++i)
{
for (j = 0; j < column; ++j)
{
printf("%d ", result[i][j]);
if (i != row -1 && j == column -1)
printf("\n");
}
}
To end also without space, stop printing space at the end.
for (i = 0; i < row; ++i)
{
for (j = 0; j < column; ++j)
{
printf(i == row -1 && j == column -1 ? "%d" : "%d ", result[i][j]);
if (i != row -1 && j == column -1)
printf("\n");
}
}
Upvotes: 0