Muzaffar Posted November 6, 2016 Posted November 6, 2016 (edited) 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 101 2 3 4 101 2 3 4 101 2 3 4 104 8 12 16So i really dont know what i should to use for make this kind of interface be free to advises.Thx Edited November 6, 2016 by Muzaffar
fiveworlds Posted November 6, 2016 Posted November 6, 2016 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(); }
LaurieAG Posted November 7, 2016 Posted November 7, 2016 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.
Sensei Posted November 8, 2016 Posted November 8, 2016 (edited) 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 November 8, 2016 by Sensei 1
fiveworlds Posted November 8, 2016 Posted November 8, 2016 (edited) Isn't simpler to write if( ( key >= '0' ) && ( key <= '9' ) ) Yeah i'm not that great at c++ and didn't want to risk it. Edited November 8, 2016 by fiveworlds
Strange Posted November 8, 2016 Posted November 8, 2016 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(). 1
fiveworlds Posted November 8, 2016 Posted November 8, 2016 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.
Strange Posted November 8, 2016 Posted November 8, 2016 (edited) 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 November 8, 2016 by Strange
fiveworlds Posted November 8, 2016 Posted November 8, 2016 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.
Strange Posted November 8, 2016 Posted November 8, 2016 It would be the same in C, C++, Python, Java, ML, Lisp ... Probably even Fortran.
fiveworlds Posted November 8, 2016 Posted November 8, 2016 (edited) 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 November 8, 2016 by fiveworlds
Strange Posted November 8, 2016 Posted November 8, 2016 OK. Point taken. We all have to learn. I'm sure that some of my code was (and probably still is) quite appalling. Good luck with your studies.
Sensei Posted November 9, 2016 Posted November 9, 2016 (edited) Yeah i'm not that great at c++ and didn't want to risk it. In JavaScript and Java it would look the same.. Edited November 9, 2016 by Sensei
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now