Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My assignment is: Write a program that asks the user to enter number 1-16 in any

ID: 439964 • Letter: M

Question

My assignment is: Write a program that asks the user to enter number 1-16 in any order, and then display those numbers in a 4 by 4 arrangement. And then take the sum of the rows, columns, and diagnols. My fourth column of numbers isnt displaying for some reason. And i dont know how to take the sums of the rows, columns or diagnols.

This is what I have:

#include<stdio.h>
int main(void)
{
int _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, _11, _12, _13, _14, _15, _16;
char s = 32;
printf("Enter the number from 1 to 16 in any order: ");
scanf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &_1, &_2, &_3, &_4, &_5, &_6,
&_7, &_8, &_9, &_10, &_11, &_12, &_13, &_14, &_15, &_16);

printf(" %d%c%d%c%d%c%d %d%c%d%c%d%c%d %d%c%d%c%d%c%d %d%c%d%c%d%c%d ", _1, s, _2, s, _3, s, _4, _5, s, _6, s, _7, s, _8,
_9, s, _10, s, _11, s, _12, s, _13, s, _14, s, _15, s, _16);
return 0;
}


Explanation / Answer

I noticed in one of the comments you said this was your first week doing programming in c. So you are not allowed to use a lot of things and are trying to get a feel for the environment of the language. So instead of just giving you and answer I'd like to just throw out some ideas for you play around and mess with to get a better feel for it.

First lets fix that problem with printing out the last line.
If you notice the pattern in your code, you print out a variable then the space character then a character. At every multiple of 4 you skip a space print. You can see this from your ..._4, _5.... and also your ..._8, _9... However if you look closely at _12 and _13 there is an s inbetween, so instead you print the %d of s, which is 32 giving you some 32's in the last line.

That is what went wrong with the last line. Now this is a tip/suggestion. There is no need to set a char to a space key, you can just press space. In other words you can remove all those %c and all those s located in your print f and it'll be fine. printf(" %d %d %d %d ") would display the spaces between the digits for you.

For the addition part, since it seems you have to code it a less efficient manner, you might just have to make variables to hold your sums. Like R1, R2, R3, R4 for the four rows, and C1,.... for the columns. Now to see which variables you're adding we can make a makeshift display of what it would look like.

1   2 3 4      R1
5   6 7 8      R2
9 10 11 12      R3
13 14 15 16    R4

C1 C2 C3 C4

Those are the variables you want to add up. So row 1 is just 1 2 3 and 4, so like you assigned your char to a number you can do the same to R1. int R1 = _1 + _2 + _3 + _4 . Now do not put this before you ask for user input or else the calculation will not work. You just have to repeat with the process with column and the diagonals. Thats all the minor tips I can think of that doesn't go too deep into more efficient code. If you have any questions just ask on comments. Also its helpful to tell us what you've learned and can use.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote