Jump to content

How to make correct output?


Muzaffar

Recommended Posts

Read an R×C matrix of integer values (R and C being predefined constants), print the same matrix with the sum of all elements of each row shown on the right and the sum of all values of each column displayed at the bottom.

int array[3][4];
int row,col,summcol,summrow[4]={0};
for(row=0;row<3;row++) {
summcol=0;
for(col=0;col<4;col++) {
scanf("%d",&array[row][col]);
summrow[col]+=array[row][col];
summcol+=array[row][col];
}
printf("The summ is%d \n",summcol);
}
for(col=0;col<4;col++) {
printf(" %d",summrow[col]);
}

 

This is the base of my code, but input and output should look like:
1 2 3 4 10
1 2 3 4 10
1 2 3 4 10
1 2 3 4 10

4 8 12 16
So i really dont know what i should to use for make this kind of interface be free to advises.Thx

Edited by Muzaffar
Link to comment
Share on other sites

Using scan_f automatically adds a newline character unfortunately. Solutions to what you want to do are windows/linux specific. On windows you could conio.h to do something like.

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
 char space = ' ';
 char key = 'a';
 int number = 0;
 int ikey;
 cout << "Enter a key" << endl;
 while (key != space) {
  key = _getch();
  if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0') {
   ikey = key - '0';number = (number * 10) + ikey;
   printf("%d", ikey);
  }  
 }
 printf("\n\n%d", number);
 key = _getch();
}
Link to comment
Share on other sites

Just add each of the elements of the row to the end of a string (after converting from numbers), add the sum (ditto) onto the end and print the string after processing each row. Do the same for the column totals at the bottom and you're done.

Link to comment
Share on other sites

  if (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0') {

 

What on Earth is that?

Isn't simpler to write

if( ( key >= '0' ) && ( key <= '9' ) )

??

 

Not to mention using isdigit() (and isalpha()/islower()/isupper() for a-z/A-Z range (I saw you used similar code for checking every character in alphabet from a...z in the past))

if( isdigit( key ) )

 

http://www.cplusplus.com/reference/cctype/isdigit/

Edited by Sensei
Link to comment
Share on other sites

 

It's ascii so that would be wrong.

 

 

It is exactly the same as what you wrote. You swerve between occasionally sensible posts and nonsense like this. Your coding examples are generally close to the worst possible solution to the problem (if not completely wrong). Thank god you are not a professional programmer.

 

Using scan_f automatically adds a newline character unfortunately.

 

It doesn't add a newline. It preserves the newline that was entered. However, as it is being used to convert a string to an integer, I struggle to see how that is relevant.

This is the base of my code, but input and output should look like:

1 2 3 4 10

1 2 3 4 10

1 2 3 4 10

1 2 3 4 10

 

4 8 12 16

So i really dont know what i should to use for make this kind of interface be free to advises.Thx

 

 

You don't say what is wrong when you run the code (and, I'm afraid, I haven't tried your code myself yet.

 

However, one problem is that you are entering 4 numbers on each line but using scan to only read a single value. I think you are unnecessarily complicating things by storing the values in a 2D array, when all you really need to keep is the array of totals. (But maybe that was part of the problem definition?)

 

You could read all four integers with a single scanf (using the format string "%d %d %d %d") but this is not very flexible and I would not recommend it. It would be better to read the input string and then split it into the substrings containing the numbers. There are various ways of doing this. Look at strtok() for example.

 

Then each string containing a number can be converted with atoi().

Link to comment
Share on other sites

Thank god you are not a professional programmer.

 

 

How does that help me become better at programming other than telling me about how you can do everything so much better than me.

 

The input looks like

1 2 3 4
The summ is10
1 2 3 4
The summ is10

OR

1
2
3
4
The summ is 10

It should look like

1 2 3 4 10
1 2 3 4 10
1 2 3 4 10
1 2 3 4 10

Inputting 1 2 3 4 automatically puts the sum i.e. (10) on the next line using scan.

Link to comment
Share on other sites

 

 

How does that help me become better at programming other than telling me about how you can do everything so much better than me.

 

 

 

1. Someone showed you a better method and you said it was wrong.

 

2. I never said I could do anything better than you.

 

 

Inputting 1 2 3 4 automatically puts the sum i.e. (10) on the next line using scan.

 

It has nothing to do with scanf.

 

As far as I can tell, you are getting a newline at the end of "1 2 3 4" because the user has to type newline to enter the information. If the requirement really is to display the sum after the entered numbers, then your solution of reading one character at a time is probably the best way. (Note that the standard function is getchar, not getch.)

 

(OK, so in a very indirect sense, it is because scanf is being used to read lines of input.)

 

However, that is such a bad idea (for example it does not allow any editing keys, such as backspace, without a lot more work), that I think the problem description may be wrong. I would guess (hope!) that the input can be read as "1 2 3 4 <newline>" but then the output shows the input values followed by the sum. Which is then a very good reason for storing the data in a 2D array.

 

So, as a very general design principle for even a simple program like this, I would recommend keeping the data input, the computation (calculating sums) and the output as quite separate bits of code. (Even doing them in separate functions to add some structure.)

 

 

// read input into array
for (int row = 0; ...
  for (int col = 0; ...
 
// Calculate sums for rows and columns
...
 
// Output array with sums for rows and columns
...

 

There is some extra overhead to that approach but I would certainly give someone more marks for clarity than efficiency in a task like this. Especially if they included something in the comments indicating that they were aware of this and, perhaps, suggesting how it could be restructured to be more efficient if required.

Edited by Strange
Link to comment
Share on other sites

Someone showed you a better method and you said it was wrong.

 

 

Well I did it the other way because I wasn't sure I could do it that way in c++ since I haven't used it for like two years. I meant I wasn't sure if it was wrong to use < with a char in c++ or not and since I don't use c++ all that often lately I don't care all too much about revising it at the moment.

Link to comment
Share on other sites

It would be the same in C, C++, Python, Java, ML, Lisp ... Probably even Fortran.

 

 

Probably I know Java is since I had to study Java recently. However it still doesn't take away from the fact that it is far more important to me to keep getting 100% in my ccna exams at the moment that learning c++ which isn't on my uni course this year. But yeah you're right to point out that I was wrong but at the same time you don't have to make comments like "thank god you aren't a professional programmer" because my brother is a professional programmer and I don't think he ever has had to use c++

Edited by fiveworlds
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.