Share via


Introduction to C++, Assignment One

I've been going back to school to ACCIS (American College of Computer Information Systems https://www.accis.edu) to finish my bachelor's degree. I am about 6 classes away from being finished. The sweet thing about taking classes at ACCIS is they are online classes which means I can work at my own pace. Some classes I burn rubber through, like Web Page Design I, Web Page Design II and Web Graphics I. Other classes I get through with all the speed of an uphill wheelchair race, like Quality Management and Enterprise Implementation Management (is that really a class?).

I've had some C classes in the past, back when I tried college right out of high school so I'm not completely new to the language. What I'm learning now, C++.NET, is a bit different from ANSI C that I learned some 12 years ago. So far, so good though. This class wasn't anything I couldn't handle and the coding was actually a lot of fun. Here's some of the code I wrote which can be a big help for anyone trying to learn C++. Note - if you're an ACCIS student, I don't condone cheating. Use the code here to get a good idea of how I solved these problems but please don't plagiarize.

1. Write a program that reads an int number from the user and then displays a message indicating whether the number is odd or even. (Note: the definition of an even number is one that is divisible by 2 without a remainder.) Use an if-else statement.

int _tmain()
{
int num1;
int num2;
cout << "Enter a number: ";
cin >> num1;
num2 = num1 % 2;
if ( num2 != 1)
cout << "Number is even (" << num1 % 2 << ")\n";
else
cout << "Number is odd (" << num1 % 2 << ")\n";
}

In this example you can figure odd or even with the mdoulus operator (%).

2. Imagine that 10 int values are labeled by position: 1, 2, 3, etc. Write a program that reads 10 integers and tracks how many of them have the same value as their position. That is, if the first number read is 1, or the third number is 3, that counts as a match, and the output would be the number of matches (from 0 to 10). Use a single if statement and a loop.

int _tmain()

{

    int numx;

      int matchcount;

      for ( int counter = 1; counter <= 10; counter++ )

      {

            cout << "Enter a number for place " << counter << ": ";

            cin >> numx;

            cout << numx << "\n";

            if ( numx == counter )

                  matchcount++;

      }

      cout << "There were " << matchcount << " matches\n";

}

 

This is a good example of numeric iteration.

 

3. Write a program that reads two floating-point values representing angles and displays a message stating which angle has the greater tangent value. Note that the trigonometric functions in the math library expect angles in radians, not degrees. 360 degrees = 2 x pi radians, so 45 degrees would be about 0.785 radians.

 

#include "stdafx.h"

using std::cout;

using std::cin;

using std::endl;

 

#include <valarray>

#using <mscorlib.dll>

#include <iomanip>

 

using namespace System;

 

int _tmain()

{

            float num1;

            float num2;

            double conv1;

            double conv2;

            double tan1;

            double tan2;

           

            cout << "Enter a first angle in degrees: ";

            cin >> num1;

            conv1 = num1 * (3.141 / 180);

            tan1 = tan( conv1 );

            cout << num1 << " degrees = " << conv1 << " radians with a TAN of " << tan1 << "\n";

 

            cout << "Enter a second angle in degrees: ";

            cin >> num2;

            conv2 = num2 * (3.141 / 180);

            tan2 = tan ( conv2 );

            cout << num2 << " degrees = " << conv2 << " radians with a TAN of " << tan2 << "\n";

 

            if ( tan1 > tan2 )

                        cout << num1 << " is greater\n";

            else

                        cout << num2 << " is greater\n";

}

 

This one took some time just to look up the algebraic equation for manually figuring Tangent. All we're doing is looking for which of two input numbers have a greater Tan(x) value where x is our input number. The easiest thing to do is look it up on a calculator. But in case you're ever held at gunpoint by thugs who are demanding such a program written in C++, you should have no problem belching out this code. You figure Tan by using pi (3.141) divided by 180. We multiply that by our input number (num1) and there you go. After that it's a straight comparison using an If/Then statement.

 

4. Write a program that reads in an integer from the keyboard and displays its value doubled, using two functions that you write. The reading function should be int and the displaying function should be void.

 

#include "stdafx.h"

using std::cout;

using std::cin;

using std::endl;

#include <valarray>

#using <mscorlib.dll>

#include <iomanip>

#include <string>

using std::string;

int x;

int inputNum();

void outputNum( int );

string ident;

using namespace System;

int _tmain()

{

      inputNum();

      outputNum ( x );

}

int inputNum()

{

      cout << "Enter a number: ";

      cin >> x;

      return x;

}

void outputNum( int x )

{

      x = x * 2;

      cout << "The number doubled is " << x << "\n";

      return;

}

 

This one is a good example of calling simple functions outside the main. Void is used because our function doesn't return a value though it does accept an integer x to work properly.

Comments