beginning programs

So my teacher isn't very good at explaining things and I understand code a little bit when I toke a class using Matlab and C++ is trickery for me however I feel lost by how to write these codes because the example he provides as references are not at all helpful. It fine if you write in pseucode or code either way

Frist one:

Write the program, rectangle.cpp, to read in the length and width of a rectangle and print
1.
The area of the rectangle

2.
The perimeter of the rectangle

3.
The diagonal across the rectangle


Assume that the values for the sides are positive but are NOT whole numbers. Be sure to prompt the user for input and label all results.

Hint: Remember the Pythagorean Theorem (a2 + b2 = c2) to calculate the diagonal.



Second problem:


other problem:


Write the program, coins.cpp, that will read in an amount of money as dollars (an integer) and cents (an integer) and then prints the total number of coins required to make the change and then the as the number of pennies, nickels, dimes, quarters and dollar coins required for the money. Your answer should the smallest number of coins needed. The following is an example of your program executing (user input is bold, red and larger):

This program will tell you how many of which coins
are needed for the amount of money you enter.

Enter number of dollars: 4
Enter number of cents: 57

9 coins are required:
4 dollars
2 quarters
0 dimes
1 nickels
2 pennies

Your output should look like the example above for ANY dollar and cent values. Be sure to prompt the user for input and label all results.

Hint : Convert the input to total number of pennies and then use the modulus (remainder) operation and integer division to calculate the results. Only use integers for this problem. Do NOT use ANY floating point (double or float) values.

Requirement: Use named constants for the value of each coin. Do not use integer constant values (like 5, 10 and 25) in the calculations, but use the named constant instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <iostream>
using namespace std;

int main ()
{
  int dollars;
  int quarters;
  int dimes;
  int nickels;
  int pennies;
  // these are the variables 

  std::cout <<"Enter number of dollars";
  cin >> dollars;
  std::cout <<"Enter number (value) of cents";
  cin >> (quarters + dimes + nickels + pennies);
  
  // should be placing amounts given 

int numberofquar = (quarters + dimes + nickels + pennies) / 25;

int numberofdimes = ((quarters + dimes + nickels + pennies) / 25) / 10;

int numberofnick =  (((quarters + dimes + nickels + pennies) / 25) / 10) / 5;

int numberofpenny = ((((quarters + dimes + nickels + pennies) / 25) / 10) / 5) / 1;

int numberofdollar = dollars;


cout <<"number of coins needed" << endl;
cout <<"dollars" << endl;
cout << numberofdollar;
cout <<"quarters" << endl;
cout << numberofquar;
cout <<"dimes" << endl;
cout << numberofdimes;
cout <<"nickels" << endl;
cout << numberofnick;
cout <<"pennies" << endl;
cout << numberofpenny;

}


this was my attempt and it didn't work anyone know how I can fix it
Last edited on
You forgot to post your attempt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
   std::cout << "Enter a character to test: ";

   char c;

   std::cin >> c;

   std::cout << "The character is: " << c << std::endl; 
   std::cout << "The ASCII value is: " << static_cast<int>( c ) << std::endl;

   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  int areaofrectanle;
  int perimeterrectangle;
  int diagonalacross;
  int length;
  int width;


  cout << "The length of the rectangle is ";
  cin >> length;
  cout << "The width of the rectangle is ";
  cin >> width;

  areaofrectangle = length * width;
  perimeterrectangle = (2 * length) * (2 * width);
  diagonalacross = sqrt(double
	  ((length ^ 2)+(width ^ 2)));

  cout << "The area of rectangle is ";  
	  cin >> areaofrectangle;
  cout << "The perimeter of the rectangle is "; 
	  cin >> perimeterrectangle;
  cout << "The diagonal across the rectangle is"; 
	  cin >> diagonalacross;

  return 0;
  }


receiving errors



1>------ Build started: Project: rectangle, Configuration: Debug Win32 ------
1>  rectangle.cpp
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(12): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(13): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(30): error C2065: 'areaofrectangle' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(33): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(36): error C2065: 'areaofrectangle' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

closed account (18hRX9L8)
Put lines 16-40 in _tmain.
Delete lines 7,14,15, and 41.
Put line 10 in line 1.
Put line 12 in line 3.
Change the cin >> in lines 34,36, and 38 to cout<<.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include "stdafx.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
  int areaofrectanle;
  int perimeterrectangle;
  int diagonalacross;
  int length;
  int width;
  int areaofrectangle = (length * width);
  int perimeterrectangle = (2 * length) * (2 * width);
  int diagonalacross = sqrt(double
	  ((length ^ 2)+(width ^ 2)));

  cout << "The length of the rectangle is ";
  cin >> length;
  cout << "The width of the rectangle is ";
  cin >> width;

  

  cout << "The area of rectangle is ";  
  cout << areaofrectangle;
  cout << "The perimeter of the rectangle is "; 
  cout perimeterrectangle;
  cout << "The diagonal across the rectangle is"; 
  cout << diagonalacross;

  return 0;
}



1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(18): error C2086: 'int diagonalacross' : redefinition
1>          c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(13) : see declaration of 'diagonalacross'
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(18): error C3861: 'sqrt': identifier not found
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(21): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(22): error C2065: 'cin' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(23): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(24): error C2065: 'cin' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(28): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(29): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(30): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(31): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(31): error C2146: syntax error : missing ';' before identifier 'perimeterrectangle'
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(32): error C2065: 'cout' : undeclared identifier
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\rectangle\rectangle\rectangle.cpp(33): error C2065: 'cout' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  
   
  int dollars;
  int quarters;
  int dimes;
  int nickels;
  int pennies;
  int cents = (quarters + dimes + nickels + pennies);
  int numberofquar = (quarters + dimes + nickels + pennies) / 25;
  int numberofdimes = ((quarters + dimes + nickels + pennies) / 25) / 10;
  int numberofnick =  (((quarters + dimes + nickels + pennies) / 25) / 10) / 5;
  int numberofpenny = ((((quarters + dimes + nickels + pennies) / 25) / 10) / 5) / 1;
  int numberofdollar = dollars;
  int coins = (numberofdollar + numberofpenny + numberofnick + numberofdimes + numberofquar);
  // these are the variables

  cout <<"Enter number of dollars";
  cin >> dollars;
  cout <<"Enter number (value) of cents";
  cin >> cents;
// should be able to place amounts given


cout <<"number of coins needed";
cout << coins;
cout << dollars;
cout << numberofdollar;
cout <<quarters;
cout << numberofquar;
cout <<dimes;
cout << numberofdimes;
cout <<nickels;
cout << numberofnick;
cout <<pennies;
cout << numberofpenny;

}



the code works however when I try to run it to make sure it works

I get this

'coins.exe': Loaded 'C:\Users\Amy\Desktop\Computer Science\Mircosoft Visual C++ Express 2010\Codes for class\temp\coins\Debug\coins.exe', Symbols loaded.
'coins.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'coins.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'coins.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'coins.exe': Loaded 'C:\ProgramData\Norton\{0C55C096-0F1D-4F28-AAA2-85EF591126E7}\NIS_20.0.0.136\Definitions\BASHDefs\20130715.001\UMEngx86.dll', Cannot find or open the PDB file
'coins.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'coins.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
Run-Time Check Failure #3 - The variable 'quarters' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'dimes' is being used without being initialized.
The thread 'Win32 Thread' (0x2930) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x47f0) has exited with code -1073741510 (0xc000013a).
The program '[17984] coins.exe: Native' has exited with code -1073741510 (0xc000013a).
Usandfriends, you said:
Delete lines 7,11,14,15, and 41.

However, line 11 was necessary because the <cmath> class includes the square root function which allows the diagonal's length to be calculated.

My attempt which is a bit less laden with variables...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>
// include cmath for the sqrt (square root function) for the diagonal length

using namespace std;


int main() {

	double w;
	double l;
	//w = width, l = length

	/*Using double instead of int because:

	-Problem assumed no whole numbers  
	-Integers do not cover decimals
	

	Using less integers/variables b/c:

	-Less disk space is taken up  
	-Math operations can be carried out in the cout line without predefined variables
	*/

	cout << "What is the width of the rectangle: ";
	cin >> w;

	cout << endl << "What is the length of the rectangle: ";
	cin >> l;

	cout << "The area of the rectangle is " << (w*l) << "." <<endl;

	cout << "The perimeter of the rectangle is " << ((2*w) + (2*l)) << "." << endl;

	cout << "The diagonal's length of the rectangle is " << sqrt((w*w)+(l*l)) << "." << endl;

	return 0;
}
so Amy
the program is running from the begin to the end, so you cannot expect to define a rule, and the value will be changed if some of the parameters are changed.
briefly,
int a, b;
int c = a + b;
a = 100;
b = 200;
this will not make c = 300, since the int c = a + b runs before you have set a = 100 and b = 200.
so line 15 - 20 is not correct.

and as the hint by your teacher, you should input two number, which are dollars and cents, let's get rid of program, consider to handle it yourself with only a pencil and a paper. so say you get 5 dollars and 31 cents, it's totally 531 cents. then how to combine them with dollar, quarter, dime, nickel, and penny.
first, 531 / 100 = dollars you need, 5, and the remind is 31, which is 531 % 100 = 31
second, 31 / 25 = quarter you need, 1, and the remind is 6
third, 6 / 10 = dime you need, 0, remind is 6
forth, 6 / 5 = nickel you need, 1, remind is 1
fifth, 1 / 1 = penny you need, 1, remind is 0
finished, right? then turns it from pencil and paper to the code, which is the answer.
Also, there are a few programming practices to think about:

Spell Check:
-debug the program by giving it a read-through and fix spelling errors

Shorten variables:
-less work
-less chance of spelling errors (see "areaofrectanle")

Defining/giving value to variables:
-You don't have to declare them and define them separately such as in the rectangle example:
1
2
int perimeterrectangle;
int perimeterrectangle = (2 * length) * (2 * width);

-Though, if you do, you can exclude the data type from the second line on which you define the value:
1
2
int perimeterrectangle;
perimeterrectangle = (2 * length) * (2 * width);


Unless you are going to use the variables later and change what they equate to, then you can simply give them a value as you declare them. In this case you don't need the variables in a later function so this route is less complicated.


Pointers:
- '^' does not raise a number to a power, it is a logical operator known as XOR, or "exclusive or" that deals with conditions/arguments, 'pow' raises numbers to a power and is included in <cmath>

Cout
-does not have to be printed each line
-if you need to put a variable next to a string, just include another operator (<<)
example:
1
2
3
int age = 20;

cout << "Your age is " << age << "." << endl;

The operator merely acts as an "insert" function to put more stuff on one line. In turn it simplifies and makes for more efficient code (and less work)!
Last edited on
Topic archived. No new replies allowed.