Question:
How do I write this C++ program?
randomchica
2011-03-18 00:39:27 UTC
This is the question I am having trouble with :

A directly connected telephone network is one in which all telephones in the network are directly connected and do not require a central switching station to establish calls between two telephones. For example, financial institutions on Wall Street use such a network to maintain direct and continuously open phone lines between firms.
The number of direct lines needed to maintain a directly connected network for n telephone is given by the formula: lines = n(n − 1) / 2 For example, directly connecting four telephones requires 6 individual lines. Adding a fifth telephone to the network would require an additional 4 lines for a total of 10 lines.Using the given formula, write a C++ program that determines the number of direct lines required for as many telephones as the user of your program desires. You should test you program to determine the number of direct lines required for 100 telephones, and the number of lines required if 10 additional new telephones were added to the network (i.e., 110 telephones).

My program looks like this:

#include
using namespace std;

int main ()

{
double lines, n;

lines = n*(n-1)/2

cout << "Enter amount of telephones:";
cin >> n;
cout << lines << endl;

return 0;

}

Please help me with what I am doing wrong! My numerical answers are never correct and a debug error always pops up when I attempt to build the program.

Thank you so much!
Five answers:
?
2011-03-18 00:49:06 UTC
you have everything you need, but you need to reorder the code. First of all, you are getting the user input after you do the computation. So you do the computation involving n, then set n to the user input, then print the answer. You never actualy use the value set to n. n ends up being set to whatever happens to be in memory when you create n. So its "randomly" set. Its good practice initialize the vairiables. such as:

double lines = 0.0;

double n = 0.0;

You are probably getting really strange answers since n is 'random' when it does the computation.



What you need to do is:

double lines, n;

cout << blah blah

cin >> n;

lines = n*(n-1) / 2 ;

cout << "lines: " << lines <
return 0;





get the users input, THEN use the value in the computation.
jamie
2011-03-18 01:08:59 UTC
try this:



#include

using namespace std;



int main ()



{

double lines, n;



cout << "Enter amount of telephones:";

cin >> n;



lines = n*(n-1)/2; //i moved this line and an error occurred because you forgot the semi colon.



cout << lines << endl;



return 0;



}
2011-03-18 05:47:06 UTC
You might get a problem (unexpected result) for n*(n-1)/2;

Reason : If n is even n-1 is odd and you are dividing odd number by 2 and getting an integer so data loss is obvious;



Change to this (n*(n-1))/2. Because division occurs before multiplication.
husoski
2011-03-18 00:47:25 UTC
You are computing lines from n BEFORE reading in the value for n!



Move that "lines = n*(n - 1)/2;" statement after the "cin >> n;" statement and things should work better.
koroly
2016-11-13 15:47:28 UTC
Why do no longer you basically upload a short lived variable? ok, basically thinknig approximately it, this works: (this might all be pseudo code by fact i'm able to't remember C) Variables would be referred to as var1 and var2. var1 = var1 + var2 var 2 = var1 - var2 var1 = var2 - var1 each and each step being completed after the previous one. desire it facilitates.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...