Introduction To The Programming Language C Computer Science Essay

Published:

C++ is an "object oriented" programming language created by Bjarne Stroustrup and released in 1985. It implements "data abstraction" using a concept called "classes", along with other features to allow object-oriented programming. Parts of the C++ program are easily reusable and extensible; existing code is easily modifiable without actually having to change the code. C++ adds a concept called "operator overloading" not seen in the earlier OOP languages and it makes the creation of libraries much cleaner. C++ maintains aspects of the C programming language, yet has features which simplify memory management. Additionally, some of the features of C++ allow low-level access to memory but also contain high level features.

discuss, to create own calculator that does the four basic arithmetic operations i.e. +, -, *, /, by using switch, -using if statements, -using if else statements. The switch statement body consists of a series of case labels and an optional default label. The if statement enables to test for a condition (such as whether two variables are equal) and branch to different parts of code, depending on the result. The else clause of an if else statement is associated with the closest previous if statement in the same scope that does not have a corresponding else statement.

Lady using a tablet
Lady using a tablet

Professional

Essay Writers

Lady Using Tablet

Get your grade
or your money back

using our Essay Writing Service!

Essay Writing Service

discuss to create "print_pyramid(...)" which takes a single integer argument "height" and displays a "pyramid" of this height made up of "*" characters on the screen. Test the function with a simple "driver" program.

Write a function "print_pyramid(...)" which takes a single integer argument "height" and displays a "pyramid" of this height made up of of "*" characters on the screen. Test the function with a simple "driver" program, which should be able to reproduce the following example output:

This program prints a 'pyramid' shape of

a specified height on the screen.

how high would you like the pyramid?: 37

Pick another height (must be between 1 and 30): 6

**

****

******

********

**********

************

QUESTION: 2

Make your own calculator that does the four basic arithmetic operations i.e. +, -, *, /.

-using switch

-using if statements

-using if else statements.

MAIN BODY

FIRST QUESTION

1. #include <iostream>

2. using namespace std;

3. void print_pyramid(int height);

4. int main()

5. {

6. int pyramid_height;

7. cout << "This program prints a 'pyramid' shape of\n";

8. cout << "a specified height on the screen.\n\n";

9. cout << "how high would you like the pyramid?: ";

10. cin >> pyramid_height;

11. while (pyramid_height > 30 || pyramid_height < 1)

12. {

13. cout << "Pick another height (must be between 1 and 30): ";

14. cin >> pyramid_height;

15. }

16. print_pyramid(pyramid_height);

17. return 0;

18. }

19. void print_pyramid(int height)

20. {

21. int line;

22. int const MARGIN = 10;

23. cout << "\n\n";

24. for (line = 1 ; line <= height ; line++)

25. {

26. int count;

27. int total_no_of_spaces = MARGIN + height - line;

28. for (count = 1 ; count <= total_no_of_spaces ; count++)

29. cout << ' ';

30. for (count = 1 ; count <= line * 2 ; count++)

31. cout << '*';

32. cout << '\n';

33. }

34. cout << "\n\n";35. }

FIGURE 1 QUESTION 1.1

LINE 1: #include <iostream> is a header file which is used for input/output in the C++ programming language.

LINE 2: These using namespace std; define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names.

LINE 3: Print sign pyramid star from certain height. Prints a pyramid of asterisks of the specified height.

LINE 4: The main function is where a program starts execution.

LINE 5: The brace shows the beginning of the main function

LINE 6: Declaration of variable pyramid_height which is integer type. Value of pyramid_height is 0.

Lady using a tablet
Lady using a tablet

Comprehensive

Writing Services

Lady Using Tablet

Plagiarism-free
Always on Time

Marked to Standard

Order Now

LINE 7: These a output statenment to print This program prints a 'pyramid' shape of and shows its in output screen

LINE 8: This a output statenment to print a specified height on the screen and shows its in output screen

LINE 9: This a output statenment to print how high would you like the pyramid? and shows its in output screen

LINE 10: These line ask the user to input a value to be assigned to the variable pyramid_height

LINE 11, 12, 13, 14,15 : While statement to check the height of the pyramid is betweed 1 to 30 if its not true its shows "Pick another height (must be between 1 and 30):and ask the user keyin again value for the pyramid_height

LINE 16: Its a funtion, the name of the function is pyramid_height

LINE 17: A way to exit a function return 0, in this case, means that the program terminated normally.

LINE 18: Then its end function

LINE 19: Function to print the height pyramid

LINE 20: The brace shows the beginning of the function

LINE 21: Declaration of variable, name line

LINE 22: Declaration of variable margin which integer aconst. Value of margin is equal to10.

LINE 23: This a output statenment to print "\n\n" and shows its in output screen

LINE 24: Line is a for looping, to loop the line =1 and if the line <= height every time

looping will plus one character from the main.

LINE 25: The brace shows the beginning of the function

LINE 26: Declaring the variable, call count

LINE 27: Declaring variable call total_no_of_spaces name which is MARGIN +

height � line;

LINE 28: Line is a for looping, to loop the count = 1 and if count <= total_no_of_spaces

every time looping will plus one character from the main.

LINE 29: These a output statenment to print � � and shows its in output screen

LINE 30: Line for looping, to loop the count = 1 and if count <= line * 2 every time looping will plus one character from the main.

LINE 31: These a output statenment to print '*'and shows its in output screen

LINE 32: These a output statenment to print '\n'and shows its in output screen

LINE 33: Then its end function

LINE 34: These a output statenment to print "\n\n"and shows its in output screen

LINE 35: Then its end function main

OUT PUT 1.1

MAIN BODY

SECOND QUESTION

1. #include <iostream>

2. using namespace std;

3. int main()

4. {

5. char op;

6. do

7. {

8. int number1, number2;

9. cout << "Please enter a number: ";

10. cin >> number1;

11. cout << "Please enter another number: ";

12. cin >> number2;

13. cout << "Press 1 to add the two numbers"

14. << endl

15. << "Press 2 to subtract the two numbers."

16. << endl

17. << "Press 3 to multiply the two numbers."

18. << endl

19. << "Press 4 to divide the two numbers."

20. << endl;

21. cin >> op;

22. switch (op)

23. {

24. case '1':

25. cout << "The result is: " << number1 + number2 << endl;

26. break;

27. case '2':

28. cout << "The result is: " << number1 - number2 << endl;

29. break;

30. case '3':

31. cout << "The result is: " << number1 * number2 << endl;

32. break;

33. case '4':

34. cout << "The result is: " << number1 / number2 << endl;

35. }

36. cout<< "do you want to another calculation? (y or n)";

37. cin >> op;

38. }

39. while (op == 'y');

40. return 0; 41. }

FIGURE 2 QUESTION 2.1

LINE 1: #include <iostream> is a header file which is used for input/output in the C++ programming language.

LINE 2: These using namespace std; define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names.

Lady using a tablet
Lady using a tablet

This Essay is

a Student's Work

Lady Using Tablet

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Examples of our work

LINE 3: Is the main function is where a program starts execution.

LINE 4: The brace shows the beginning of the main function

LINE 5: Declaring character, name op

LINE 6: Its is a do looping

LINE 7: The brace shows the beginning of the function

LINE 8: Declaring variables, name number1 and number2

LINE 9: These a output statenment to print "Please enter a number: " and shows its in output screen

LINE 10: These line ask the user to input a value to be assigned to the variable number1

LINE 11: These a output statenment to print "Please enter another number: " and shows its in output screen

LINE 12: These line ask the user to input a value to be assigned to the variable number2

LINE 13: These a output statenment to print "Press 1 to add the two numbers" and shows its in output screen

LINE 14: New line

LINE 15: These a output statenment to print "Press 2 to subtract the two numbers" and shows its in output screen

LINE 16: New line

LINE 17: These a output statenment to print "Press 3 to multiply the two numbers" and shows its in output screen

LINE 18: New line

LINE 19: These a output statenment to print "Press 4 to divide the two numbers" and shows its in output screen

LINE 20: New line

LINE 21: These line ask the user to input a value to be assigned to the variable op

LINE 22: It�s a switch statement

LINE 23: The brace shows the beginning of the function

LINE 24: case when op = 1

LINE 25: These a output statenment to print "The result is: " by adding number1 and number2 and shows its in output screen

LINE 26: In a switch statement, break causes the program to execute the next statement after the switch.

LINE 27: case when op = 2

LINE 28: These a output statenment to print "The result is: " by subtracting number1 and number2 and shows its in output screen

LINE 29: In a switch statement, break causes the program to execute the next statement after the switch.

LINE 30: case when op = 3

LINE 31: These a output statenment to print "The result is: " by multiplying number1 and number2 and shows its in output screen

LINE 32: In a switch statement, break causes the program to execute the next statement after the switch.

LINE 33: case when op = 4

LINE 34: These a output statenment to print "The result is: " by divide number1 and number2 and shows its in output screen

LINE 35: Then its end function

LINE 36: These a output statenment to print "do you want to another calculation? (y or n)"and shows its in output screen

LINE 37: These line ask the user to input a value to be assigned to the op is y or n

LINE 38: Then its end function

LINE 39: while loop when op == 'y'

LINE 40: A way to exit main function with return 0, in this case, means that the program terminated normally.

LINE 41: Then its end function main

OUT PUT: 2.1

QUESTION 2.2

1. #include <iostream>

2. using namespace std;

3. void main ()

4. {

5. float number1, number2;

6. int which_one;

7. float result;

8. char op;

9. do

10. {

11. cout << "Please enter a number : ";

12. cin >> number1;

13. cout << "Please enter another number : ";

14. cin >> number2;

15. cout << "Press 1 to add the two numbers"

16. << endl

17. << "Press 2 to subtract the two numbers."

18. << endl

19. << "Press 3 to multiply the two numbers."

20. << endl

21. << "Press 4 to divide the two numbers."

22. << endl;

23. cin >> which_one;

24. if (which_one == 1)

25. {

26. result = number1 + number2;

27. cout << "The result is " << result << endl;

28. }

29. if (which_one == 2)

30. {

31. result = number1 - number2;

32. cout << "The result is " << result << endl;

33. }

34. if (which_one == 3)

35. {

36. result = number1 * number2;

37. cout << "The result is " << result << endl;

38. }

39. if (which_one == 4)

40. {

41. result = number1 / number2;

42. cout << "The result is " << result << endl;

43. }

44. cout<< "do you want to another calculation? (y or n)";

45. cin >> op;

46. }

47. while (op == 'y');

48. }

FIGURE 2 QUESTION 2.2

LINE 1: #include <iostream> is a header file which is used for input/output in the C++ programming language.

LINE 2: These using namespace std; define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names.

LINE 3: Is the main function is where a program starts execution.

LINE 4: The brace shows the beginning of the main function

LINE 5: Declaration of variable name number1, number2 which is float type.

LINE 6: Declaration of variable name which_one which is integer type.

LINE 7: Declaration of variable name result which is float type

LINE 8: Declaring character, name op

LINE 9: Its is a do looping

LINE 10: The brace shows the beginning of the function

LINE 11:These a output statenment to print "Please enter a number: " and shows its in output screen

LINE 12: These line ask the user to input a value to be assigned to the variable number1

LINE 13: These a output statenment to print "Please enter another number: " and shows its in output screen

LINE 14: These line ask the user to input a value to be assigned to the variable number2

LINE 15: These a output statenment to print "Press 1 to add the two numbers" and shows its in output screen

LINE 16: New line

LINE 17: These a output statenment to print "Press 2 to subtract the two numbers" and shows its in output screen

LINE 18: New line

LINE 19: These a output statenment to print "Press 3 to multiply the two numbers" and shows its in output screen

LINE 20: New line

LINE 21: These a output statenment to print "Press 4 to divide the two numbers" and shows its in output screen

LINE 22: New line

LINE 23: These line ask the user to input a value to be assigned to the variable which_one

LINE 24: If the which one = 1 the result will add number 1 and number2

LINE 25: The brace shows the beginning of the function

LINE 26: This line the result will add number1 and number2

LINE 27: These a output statenment to print "The result is: " by adding number1 and number2 and shows its in output screen

LINE 28: Then its end function

LINE 29: If the which one = 2 the result will subtrack number 1 and number2

LINE 30: The brace shows the beginning of the function

LINE 31: This line the result will subtrack number1 and number2

LINE 32: These a output statenment to print "The result is: " by subtrackting number1 and number2 and shows its in output screen

LINE 33: Then its end function

LINE 34: If the which one = 3 the result will multiply number 1 and number2

LINE 35: The brace shows the beginning of the function

LINE 36: This line the result will multiply number1 and number2

LINE 37: These a output statenment to print "The result is: " by multiplying number1 and number2 and shows its in output screen

LINE 38: Then its end function

LINE 39: If the which one = 4 the result will devide number 1 and number2

LINE 40: The brace shows the beginning of the function

LINE 41: This line the result will devide number1 and number2

LINE 42: These a output statenment to print "The result is: " by deviding number1 and number2 and shows its in output screen

LINE 43: Then its end function

LINE 44: These a output statenment to print "do you want to another calculation? (y or n)"and shows its in output screen

Line 45: These line ask the user to input a value to be assigned to the op is y or n

Line 46 : Then its end function

Line 47 : while loop when op == 'y'

Line 48 : Then its end function main

OUT PUT: 2.2

QUESTION 2.3

1. #include <iostream>

2. using namespace std;

3. void main ()

4. {

5. float number1, number2;

6. int which_one;

7. float result;

8. char op;

9. do

10. {

11. cout << "Please enter a number : ";

12. cin >> number1;

13. cout << "Please enter another number : ";

14. cin >> number2;

15. cout << "Press 1 to add the two numbers"

16. << endl

17. << "Press 2 to subtract the two numbers."

18. << endl

19. << "Press 3 to multiply the two numbers."

20. << endl

21. << "Press 4 to divide the two numbers."

22. << endl;

23. cin >> which_one;

24. if (which_one == 1)

25. {

26. result = number1 + number2;

27. cout << "The result is " << result << endl;

28. }

29. else if (which_one == 2)

30. {

31. result = number1 - number2;

32. cout << "The result is " << result << endl;

33. }

34. else if (which_one == 3)

35. {

36. result = number1 * number2;

37. cout << "The result is " << result << endl;

38. }

39. else if (which_one == 4)

40. {

41. result = number1 / number2;

42. cout << "The result is " << result << endl;

43. }

44. cout<< "do you want to another calculation? (y or n)";

45. cin >> op;

46. }

47. while (op == 'y'); 48. }

FIGURE 2 QUESTION 2.3

LINE 1: #include <iostream> is a header file which is used for input/output in the C++ programming language.

LINE 2: These using namespace std; define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names.

LINE 3: Is the main function is where a program starts execution.

LINE 4: The brace shows the beginning of the main function

LINE 5: Declaration of variable name number1, number2 which is float type.

LINE 6: Declaration of variable name which_one which is integer type.

LINE 7: Declaration of variable name result which is float type

LINE 8: Declaring character, name op

LINE 9: Its is a do looping

LINE 10: The brace shows the beginning of the function

Line 11 :These a output statenment to print "Please enter a number: " and shows its in output screen

LINE 12: These line ask the user to input a value to be assigned to the variable number1

LINE 13: These a output statenment to print "Please enter another number: " and shows its in output screen

LINE 14: These line ask the user to input a value to be assigned to the variable number2

LINE 15: These a output statenment to print "Press 1 to add the two numbers" and shows its in output screen

LINE 16: New line

LINE 17: These a output statenment to print "Press 2 to subtract the two numbers" and shows its in output screen

LINE 18: New line

LINE 19: These a output statenment to print "Press 3 to multiply the two numbers" and shows its in output screen

LINE 20: New line

LINE 21: These a output statenment to print "Press 4 to divide the two numbers" and shows its in output screen

LINE 22: New line

LINE 23: These line ask the user to input a value to be assigned to the variable which_one

LINE 24: If else the which one = 1 the result will add number 1 and number2

LINE 25: The brace shows the beginning of the function

LINE 26: This line the result will add number1 and number2

LINE 27: These a output statenment to print "The result is: " by adding number1 and number2 and shows its in output screen

LINE 28: Then its end function

LINE 29: If else the which one = 2 the result will subtrack number 1 and number2

LINE 30: The brace shows the beginning of the function

LINE 31: This line the result will subtrack number1 and number2

LINE 32: These a output statenment to print "The result is: " by subtrackting number1 and number2 and shows its in output screen

LINE 33: Then its end function

LINE 34: If else the which one = 3 the result will multiply number 1 and number2

LINE 35: The brace shows the beginning of the function

LINE 36: This line the result will multiply number1 and number2

LINE 37: These a output statenment to print "The result is: " by multiplying number1 and number2 and shows its in output screen

LINE 38: Then its end function

LINE 39: If else the which one = 4 the result will devide number 1 and number2

LINE 40: The brace shows the beginning of the function

LINE 41: This line the result will devide number1 and number2

LINE 42: These a output statenment to print "The result is: " by deviding number1 and number2 and shows its in output screen

LINE 43: Then its end function

LINE 44: These a output statenment to print "do you want to another calculation? (y or n)"and shows its in output screen

Line 45: These line ask the user to input a value to be assigned to the op is y or n

Line 46 : Then its end function

Line 47: while loop when op == 'y'

Line 48 : Then its end function main

OUT PUT: 2.3

CONCLUSION

Question 1 discuss, to create own calculator that does the four basic arithmetic operations i.e. +, -, *, /, by using switch, -using if statements, -using if else statements. The switch statement body consists of a series of case labels and an optional default label. The if statement enables to test for a condition (such as whether two variables are equal) and branch to different parts of code, depending on the result. The else clause of an if else statement is associated with the closest previous if statement in the same scope that does not have a corresponding else statement. From these question get better understanding on switch statement, if statement and if else statement and also can improve the level these statements.

Question 2 discuss to create "print_pyramid(...)" which takes a single integer argument "height" and displays a "pyramid" of this height made up of of "*" characters on the screen. Test the function with a simple "driver" program. From these question to get know how to create "print_pyramid(...)" which takes a single integer argument "height" and displays a "pyramid" of this height made up of of "*" characters on the screen and also get better understanding on these question.