This is a reasonably good first start. I think it could be improved a little.
Functions
#Functions YouYou have a lot of redundant code, and a lot of different types of code mixed together. Your programs will be easier to understand and maintain, and possibly smaller and faster, if you break them up into functions that handle the various tasks you need to do.
Others have mentioned getting the input from the user as a function you could make. That's a good idea. It looks to me like you have several places where you are outputting the results, too. This code should be separate from the code that calculates the results. This is often called separation of concerns, or keeping your business logic separate from your display logic. Furthermore, if you've made a mistake in the display code, you only have to fix it in one place. If you have the same code in multiple places and you discover an error in it, you now have to check each place you have duplicate code to see if you made the same error. It quickly becomes difficult to maintain.
Formatting
#Formatting
AnotherAnother thing that makes code easier to read, change, and fix is to format it with those goals in mind. While C allows you to omit the braces when the body of a for
loop or if
class is a single line, this is asking for trouble. Later, when the code needs to be modified, it's easy to forget that there are no braces, add a line with the same indentation and have code that doesn't do what you think it does. For that reason, I recommend always adding braces around every for
, if
, do
, and while
body, even if it's only a single line.
Also, your brace style is inconsistent. In some cases you put the opening brace on the same line as the function name or for
statement, and in other cases it's below it. It doesn't matter which you use, but it's a good idea to be consistent and use the same one in all cases.
It's best to create variables as close to their first use as you can. This makes it easier to see and understand the lifetime of a variable. It can also help the compiler to optimize things if it knows it doesn't need to use all 15 variables at once in a given function.
Avoid Magic Numbers
#Avoid Magic Numbers YouYou have the number 10 in the code something like 39 times. If you ever decide you need to change the maximum size to something else, you have to change it in 39 places. If you used a named constant, you'd only have to change it one place and recompile. You could put this at the top of the file:
const size_t kMaxRowSize = 10;
const size_t kMaxColSize = 10;
Then declare your variables like this:
int array[kMaxRowSize][kMaxColSize];
A good rule of thumb is that any time you have a bare number other than 0 or 1 in your code, you probably want to make a named constant for it.
This is a reasonably good first start. I think it could be improved a little.
#Functions You have a lot of redundant code, and a lot of different types of code mixed together. Your programs will be easier to understand and maintain, and possibly smaller and faster, if you break them up into functions that handle the various tasks you need to do.
Others have mentioned getting the input from the user as a function you could make. That's a good idea. It looks to me like you have several places where you are outputting the results, too. This code should be separate from the code that calculates the results. This is often called separation of concerns, or keeping your business logic separate from your display logic. Furthermore, if you've made a mistake in the display code, you only have to fix it in one place. If you have the same code in multiple places and you discover an error in it, you now have to check each place you have duplicate code to see if you made the same error. It quickly becomes difficult to maintain.
#Formatting
Another thing that makes code easier to read, change, and fix is to format it with those goals in mind. While C allows you to omit the braces when the body of a for
loop or if
class is a single line, this is asking for trouble. Later, when the code needs to be modified, it's easy to forget that there are no braces, add a line with the same indentation and have code that doesn't do what you think it does. For that reason, I recommend always adding braces around every for
, if
, do
, and while
body, even if it's only a single line.
Also, your brace style is inconsistent. In some cases you put the opening brace on the same line as the function name or for
statement, and in other cases it's below it. It doesn't matter which you use, but it's a good idea to be consistent and use the same one in all cases.
It's best to create variables as close to their first use as you can. This makes it easier to see and understand the lifetime of a variable. It can also help the compiler to optimize things if it knows it doesn't need to use all 15 variables at once in a given function.
#Avoid Magic Numbers You have the number 10 in the code something like 39 times. If you ever decide you need to change the maximum size to something else, you have to change it in 39 places. If you used a named constant, you'd only have to change it one place and recompile. You could put this at the top of the file:
const size_t kMaxRowSize = 10;
const size_t kMaxColSize = 10;
Then declare your variables like this:
int array[kMaxRowSize][kMaxColSize];
A good rule of thumb is that any time you have a bare number other than 0 or 1 in your code, you probably want to make a named constant for it.
This is a reasonably good first start. I think it could be improved a little.
Functions
You have a lot of redundant code, and a lot of different types of code mixed together. Your programs will be easier to understand and maintain, and possibly smaller and faster, if you break them up into functions that handle the various tasks you need to do.
Others have mentioned getting the input from the user as a function you could make. That's a good idea. It looks to me like you have several places where you are outputting the results, too. This code should be separate from the code that calculates the results. This is often called separation of concerns, or keeping your business logic separate from your display logic. Furthermore, if you've made a mistake in the display code, you only have to fix it in one place. If you have the same code in multiple places and you discover an error in it, you now have to check each place you have duplicate code to see if you made the same error. It quickly becomes difficult to maintain.
Formatting
Another thing that makes code easier to read, change, and fix is to format it with those goals in mind. While C allows you to omit the braces when the body of a for
loop or if
class is a single line, this is asking for trouble. Later, when the code needs to be modified, it's easy to forget that there are no braces, add a line with the same indentation and have code that doesn't do what you think it does. For that reason, I recommend always adding braces around every for
, if
, do
, and while
body, even if it's only a single line.
Also, your brace style is inconsistent. In some cases you put the opening brace on the same line as the function name or for
statement, and in other cases it's below it. It doesn't matter which you use, but it's a good idea to be consistent and use the same one in all cases.
It's best to create variables as close to their first use as you can. This makes it easier to see and understand the lifetime of a variable. It can also help the compiler to optimize things if it knows it doesn't need to use all 15 variables at once in a given function.
Avoid Magic Numbers
You have the number 10 in the code something like 39 times. If you ever decide you need to change the maximum size to something else, you have to change it in 39 places. If you used a named constant, you'd only have to change it one place and recompile. You could put this at the top of the file:
const size_t kMaxRowSize = 10;
const size_t kMaxColSize = 10;
Then declare your variables like this:
int array[kMaxRowSize][kMaxColSize];
A good rule of thumb is that any time you have a bare number other than 0 or 1 in your code, you probably want to make a named constant for it.
This is a reasonably good first start. I think it could be improved a little.
#Functions You have a lot of redundant code, and a lot of different types of code mixed together. Your programs will be easier to understand and maintain, and possibly smaller and faster, if you break them up into functions that handle the various tasks you need to do.
Others have mentioned getting the input from the user as a function you could make. That's a good idea. It looks to me like you have several places where you are outputting the results, too. This code should be separate from the code that calculates the results. This is often called separation of concerns, or keeping your business logic separate from your display logic. Furthermore, if you've made a mistake in the display code, you only have to fix it in one place. If you have the same code in multiple places and you discover an error in it, you now have to check each place you have duplicate code to see if you made the same error. It quickly becomes difficult to maintain.
#Formatting
Another thing that makes code easier to read, change, and fix is to format it with those goals in mind. While C allows you to omit the braces when the body of a for
loop or if
class is a single line, this is asking for trouble. Later, when the code needs to be modified, it's easy to forget that there are no braces, add a line with the same indentation and have code that doesn't do what you think it does. For that reason, I recommend always adding braces around every for
, if
, do
, and while
body, even if it's only a single line.
Also, your brace style is inconsistent. In some cases you put the opening brace on the same line as the function name or for
statement, and in other cases it's below it. It doesn't matter which you use, but it's a good idea to be consistent and use the same one in all cases.
It's best to create variables as close to their first use as you can. This makes it easier to see and understand the lifetime of a variable. It can also help the compiler to optimize things if it knows it doesn't need to use all 15 variables at once in a given function.
#Avoid Magic Numbers You have the number 10 in the code something like 39 times. If you ever decide you need to change the maximum size to something else, you have to change it in 39 places. If you used a named constant, you'd only have to change it one place and recompile. You could put this at the top of the file:
const size_t kMaxRowSize = 10;
const size_t kMaxColSize = 10;
Then declare your variables like this:
int array[kMaxRowSize][kMaxColSize];
A good rule of thumb is that any time you have a bare number other than 0 or 1 in your code, you probably want to make a named constant for it.