Posts

Showing posts from March, 2019

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 2

Image
2. Write a C++ program to swap first and last element of an integer 1-d array. This assignment was not too difficult, but it did take some time for me to figure how to reference the last element value.  Luckily, I was able to simply use "x-1" in order to reference the last value.  Nevertheless, I have continued to use my base template in regards to coding style.  Everything has been broken into functions.  Also, I used a lot of the same code from the prior assignment in order to create this assignment.  It is important to note that I commented out the initial test code to ensure I understood how to execute this task at a very base minimal level.  Learning from the prior assignment, I reused the temp1/temp2 variables in order to store the values accordingly. Here is the sample source code for comparison.

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 1

Image
1. Write a C++ program to find the sum and average of one dimensional integer array. For the past 10 assignments, I have began to standardize my code format by including a boilerplate design.  Also, I have began including comments as a header as well.  For this assignment, I have began to further enhance my coding format, specifically by breaking chunks of the code into functions.  Although it takes a bit more time to code, the flexibility it creates in regards to updating the code is quite remarkable.  With that said, I will be breaking down the screenshots of my source into sizable sections for increased clarity. ***Spoiler Alert: Although I did create a program that takes the sum and average of a one dimensional integer array, it was a limited implementation.  After review the sample source code, I modified it to become a more intuitive and robust system.*** For this assignment, I wanted to go a bit beyond the original requirements of building a s...

CPPForSchool.com: User Defined Function Set 2 Assignment 1

Image
1. Write a program that lets the user perform arithmetic operations on two numbers. Your program must be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numbers. Furthermore, your program must consist of following functions: 1. Function showChoice: This function shows the options to the user and explains how to enter data. 2. Function add: This function accepts two number as arguments and returns sum. 3. Function subtract: This function accepts two number as arguments and returns their difference. 4. Function multiply: This function accepts two number as arguments and returns product. 5. Function divide: This function accepts two number as arguments and returns quotient. ------------------------------------------------------------------------------------------------------------------------ Just like when I was in school, it appears the more complex the assignment, the greater chances I have of ...

CPPForSchool.com: User Defined Function Set 1: Assignment 11

Image
11. Write the output of the following program:        #include <iostream> using namespace std; int func(int &x, int y = 10) { if (x % y == 0) return ++x; else return y--; } int main() { int p = 20, q = 23; q = func(p, q); cout << p << " " << " " << q << endl; p = func (q); cout << p << " " << " " << q << endl; q = func (p); cout << p << " " << " " << q << endl; return 0; } ----------------------------------------------- 20 23 10 23 11 11

CPPForSchool.com: User Defined Function Set 1: Assignment 10

Image
10. Write the output of the following program : #include <iostream> using namespace std; static int i = 100; void abc() { static int i = 8; cout << "first = " << i++ << endl; } int main() { static int i = 2; abc(); cout << "second = " << i << endl; abc(); return 0; } ----------------------------------------------------- first = 8 second = 2 first = 9

CPPForSchool.com: User Defined Function Set 1: Assignment 9

Image
9. Give the output of the following program. #include <iostream> using namespace std; int global = 10; void func(int &x, int y) { x = x - y; y = x * 10; cout << x << ", " << y << '\n'; } int main() { int global = 7; func (::global, global); cout << global << ", " << ::global << '\n'; func(global, ::global); cout << global << ", " << ::global << '\n'; return 0; } ------------------------------------------------- 3, 100 7, 30 4, 40 4, 3

CPPForSchool.com: User Defined Function Set 1: Assignment 8

Image
8. Write the output of the following program:  #include <iostream> using namespace std; void Execute(int &B, int C = 100) { int temp = B + C; B += temp; if (C == 100) cout << temp << " " << B << " " << C << endl; } int main() { int M = 90, N = 10; Execute(M); cout << M << " " << N << endl; Execute(M, N); cout << M << " " << N << endl; return 0; } -------------------------------------------------------- 190 280 100 280 10 570 10

CPPForSchool.com: User Defined Function Set 1: Assignment 7

Image
7. Write the output of the following program: #include <iostream> using namespace std; void X(int A, int &B) { A = A + B; B = A - B; A = A - B; } int main() { int a = 4, b = 18; X(a,b); cout << a << ", " << b; return 0; } ------------------------------------------------------------------------------------------- 4, 4

CPPForSchool.com: User Defined Function Set 1: Assignment 6

Image
6. Write the output of the following program : #include <iostream> using namespace std; void X(int &A, int &B) { A = A + B; B = A - B; A = A - B; } int main() { int a = 4, b = 18; X(a,b); cout << a << ", " << b; return 0; } -------------------------------------------------------------------- Result: 18, 4

CPPForSchool.com: User Defined Function Set 1: Assignment 5

Image
5. Write a function called zero_small() that has two integer arguments being passed by reference and sets the smaller of the two numbers to 0. Write the main program to access the function.  This assignment was not too difficult, but I did run into a minor issue in regards to declaring the references, specifically with a and b.  I was under the impression that the & would be utilized throughout all of the code.  However, I was quickly reminded that it is only within the initial declaration, not every time the variable is referenced.  Once I cleared that up, everything ran smoothly.  It appears that the sample source code is much cleaner than my approach, but I believe that my version is a bit more user friendly, in regards to presentation.

CPPForSchool.com: User Defined Function Set 1: Assignment 4

Image
4. Raising a number to a power p is the same as multiplying n by itself p times. Write a function called power that takes two arguments, a double value for n and an int value for p, and return the result as double value. Use default argument of 2 for p, so that if this argument is omitted the number will be squared. Write the main function that gets value from the user to test power function. This assignment had me quite conflicted.  I say this because I got so used to using the math.h library, especially for calculating exponents.  Nevertheless, this was quite the learning process.  Admittedly, I was able to create the proper loop and algorithm, but my variable declarations were off.  Due to the miss-declarations, the resulting product was consistently wrong and way off base (no pun intended).  Nevertheless, once I reviewed the sample code, I was able to modify my source and successfully outputted the expected results.  Below are screenshots ...

CPPForSchool.com: User Defined Function Set 1: Assignment 3

Image
3. Write a function that receives two numbers as an argument and display all prime numbers between these two numbers. Call this function from main( ). It seems that finding prime numbers is not my forte, yet.  I experienced quite a snag trying to implement the correct algorithm in order to separate prime numbers from the rest of the integers.  On the other hand, I am beginning to gain more confidence in my use of functions being called from outside of int main.  Below is my source code and outputs along with the sample source and output for comparison.

CPPForSchool.com: User Defined Function Set 1: Assignment 2

Image
2. Write a function to calculate the factorial value of any integer as an argument. Call this function from main( ) and print the results in main( ). This assignment was a bit of an eye opener.  In the past, I have gotten rather accustomed to return 0 and utilizing custom void functions.  For this assignment, I had to ensure that I returned the factorial in order for the values to be relayed into int main().  Nevertheless, I had a pretty good time coding this exercise.  Below is my source code and outputs.  Also, the sample source and output has been posted for comparison.

CPPForSchool.com: User Defined Function Set 1: Assignment 1

Image
1. Write a program using function which accept two integers as an argument and return its sum. Call this function from main( ) and print the results in main( ). It appears that the ongoing theme of different coding styles is upon us.  Unlike the sample source code, I chose not to return anything within the User Defined Function, specifically for sumOfNums().  Also, to help reinforce what has occurred, I included an output of "x+y=sum", instead of just simply outputting the answer.  Below is my source code and output.  Lastly, I have included the sample source code and output. ***Although my source code did not follow the instructions in regards to printing within int main(), I went ahead forward because the concept is not lost on me.  I will implement the function as instructed within later assignments.  (I am just extremely eager to move forward)***

CPPForSchool.com: Flow of Control Set 3: Assignment 3

3. Write a program to compute the cosine of x. The user should supply x and a positive integer n. We compute the cosine of x using the series and the computation should use all terms in the series up through the term involving x n cos x = 1 - x 2 /2! + x 4 /4! - x 6 /6! ... It is quite frustrating to run into the unknown and unable to decipher my lack of comprehension to the subject at hand.  Although, I understand conceptually what cos x is, it does not translate to C++.  Although, things may be different if I am able to use the math library in C++, this assignment is supposed to be answered utilizing loops.  Like the prior assignment, I will have to omit any code and simply post the sample source code provided by CPPForSchool . #include <iostream> using namespace std ; int main () { int i , j , n , fact , sign =- 1 ; float x , p , sum = 0 ; cout << "Enter the value of x : " ; cin >> x ; cout << "Ent...

CPPForSchool.com: Flow of Control Set 3: Assignment 2

2. Write a program to compute sinx for given x. The user should supply x and a positive integer n. We compute the sine of x using the series and the computation should use all terms in the series up through the term involving x n sin x = x - x 3 /3! + x 5 /5! - x 7 /7! + x 9 /9! ....... It appears that my math nightmare of the past have come back to haunt me!  All joking aside, it has been over 10 years since I approached any math problems containing sin, cos, tan, etc.  Knowing that I am supposed to use loops for this assignment, I am extremely lost.  Of course, the simple route would be to utilize the math library, but in regards to this exercise it is not an option.  Sadly, even reviewing the sample source code has left me out in the cold.  Nevertheless, I have went ahead and copied the sample source code below. #include <iostream> using namespace std ; int main () { int i , j , n , fact , sign =- 1 ; float x , p , s...

CPPForSchool.com: Flow of Control Set 3: Assignment 1

Image
1. Write a program to print following : i) ********** ********** ********** ********** ii) * ** *** ** * * **** *  iii)         *       **     ***   ** * * **** * iv)         *       ***     *** **   ** * * ** * **** * ** ** v)         1       222     33333   4444444 555555555 vi)         1       212     32123   4321234...