Hi! This is not a tutorial, though I'll explain any code I post here. On this blog I post source codes of various programs I have written, though the questions/programming problems may or may not be my own. These include solutions to some questions of some good books too. Not too language specific, since I'll be posting codes in C, C++, Java, C#, etc. For contact information, see my Disclosure Policy, for more info, check out my first post: http://letsplaycoding.blogspot.com/2014/03/welcome.html
#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H
int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.
printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.
scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.
int arr1[25], arr2[25], i, j; -> Here we define the variables we are going to use in our program.
arr1[25], arr2[25] hold the user input numbers and reversed array respectively.
i, j are used as the loop variables to loop through the array.
Here we loop through both arrays and do the required things. Look at the for loop definition. We have used the "," (comma) operator to initialise and increment multiple variables. This saves a few lines of code. Loop variables are initialised in one go and incrmeented/decremented in one go as well.
We then take user input to build first array. In the next step we copy that value in the last element of the second array. The loop variable "i" traverses "arr1" from left to right, and loop variable "j" traverses "arr2" from right to left. Hence we increment "i" and decrement "j".
Here we just loop through both arrays and print them for reference.
system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.
#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H
int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.
printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.
scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.
int elements[25], loopCounter, positiveCount = 0, negativeCount = 0, evenCount = 0, oddCount = 0; -> Here we define the variables we are going to use in our program.
elements[25] hold the user input numbers.
loopCounter is used as the loop variable to loop through the array. The variables positiveCount, negativeCount, evenCount, oddCount are used to track the count of each type of number.
Here we loop through the entire array, get user input, and each input is checked whether it's odd/even/negative/positive. Appropriate count variable is incremented to store the count.
printf ("\nNumber of Positive Numbers: %d", positiveCount);
printf ("\nNumber of Negative Numbers: %d", negativeCount);
printf ("\nNumber of Even Numbers: %d", evenCount);
printf ("\nNumber of Odd Numbers: %d\n", oddCount);
After the loop, here we print the values of each variable to give the output stating the count of each number type in the user input.
system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.