In summary, dduardo provides a summary of the content in the following conversation. He also provides a suggestion for clarification.
  • #71
if this was a wrong place to put my question, can you tell me where? Thanks
 
Technology news on Phys.org
  • #72
I will do it by keeping two variables,
1. min_pos - will hold the position of first 7
2. max_pos - will hold the position of last 7

Steps :
1. Initialise min_pos and max_pos to -1
2. Whenever a 7 is read,
if min_pos = -1, update min_pos = counter
if counter > max_pos, update max_pos = counter
3. if both min_pos = -1, output no 7 was read
otherwise output min_pos and max_pos

Once you are done with the program, read the program carefully. Try to understand how the value of -1 was being used here. Such techniques are commonplace in many programs, so it should help you out.

-- AI
 
  • #73
I need help with tutorial.I'am working a assigment 1.I enterd this in the Bloodshed Dev C++.
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    char myCharacter = 'z';
    cout << "The datatype is" << sizeof(int) << " bytes!" << endl;
    cout << " the varible has a value of " << myCharacter << endl;
    return 0;

}
It works kind of.For some starge reason the box pops up but it closes as soon as it opens.
 
  • #74
scott1, you can either run the application directly from thecommand line or add the following:

This should go at the top:

#include <cstdlib>

and this should go right before the return line:

system("PAUSE");
 
  • #75
It works now.Thanks
 
  • #76
I'am working on the Pythegrom therom assigment.I need help
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  int a = 'a', b = 'b', c = 'c';
     cout << "Enter Length of A" << endl;
     cin >>  a;
     cout << "enter length of b" << endl;
     cin >> b;
  a * a * b * b == c * c
   cout  << "c =" <<  c << endl;
    system("PAUSE");
    return EXIT_SUCCESS
It says there somthing worng with line 13 and 4 things with line 15 but I think there's probally more stuff worng with it then that.
 
  • #77
Why are you doing a = 'a', b='b', c='c'?

Why don't you just do: double a, b, c;

Then the pythagorean theorem is simply c = sqrt(a*a+b*b) ;

remember to #include <cmath> for the sqrt function.
 
  • #78
It worked thanks
 
  • #79
Is there ever going to be another more advance tutoril
 
  • #80
I've don't this before and now I'm trying to do the hypotenuse thing again...here's what I've got:

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
   double a, b, c;
   
   a = sqrt(b*b+c*c);
   
   cin >> b >> c;
   cout << a;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

every time I run it a is equal to 0 EDIT: I'm stupid, I got it..
 
  • #81
I the "cin" needs to go before the equation.
 
  • #82
some problems about matrix class

I have an exercise of building matrix class with characters: plus, subtract, divide, convert. Can you help me?:confused:
 
  • #83
I suppose I may have posted this out of place, but it seemed to go beyond the tutorial. Here it is again.

I've been trying for days to get my boyfriend's C++ program to work, and I am almost ready to concede defeat, but I have frequented this site in the past as a lurker, actually learning a lot of what I know from the tutorials (thanks!).

Now, I have a problem I just can't seem to solve.

The object is to have the user pick a choose from a menu and have that value stored somewhere to be used by a function later. Once that choice is picked, the user needs to be able to pick another choice, until he or she fills in all three choices and then it's on to the formula! I also need a choice for clearing the data (I know how to set it all to 0) and one for quitting the program (just return 0; right?), as well as a display of the data that has been entered already once the formula calculates (I have this block of code written too). My formula works, but I'm not sure how to get all the values stored or how to let the user go back and pick a choice from the list after he or she has chosen one already.

Here's what I have so far, and it's a work in progress, but hopefully it's on the right track. I'm thinking I need to, maybe, use a switch statement for the menu, but I can't get that to work for me, and I'm still not sure how to store a value that's not going to be immediately used (maybe something with an array, but again I seem unable to produce the right code).


#include <iostream>
#include <math.h>
using namespace std;

doublemortgage (int c, double b, double a)
{
int i = c;
double j = b;
double k = a;
double l = b/(12 * 100);
double m = c * 12;
double result = k * (l / (1 - pow(1+l, -m)));
return result;
}


int main ()
{
int c;
double b;
double a;
double result;
int x;

cout << "Mortgage Calculation Menu \n";
cout << "1. Enter a Loan Amount \n";
cout << "2. Enter a Loan Rate \n";
cout << "3. Enter a Term in Years \n";
cout << "4. Calculate a Payment \n";
cout << "5. Clear All Input \n";
cout << "9. Quit \n";
cout << "Enter Your Selection: ";
cin >> x;

cout << " \n";

cout << "Enter a Loan Amount: $";
cin >> a;
cout << "Enter a Loan Rate: ";
cin >> b;
cout << "Enter a Term in Years: ";
cin >> c;
cout << "Your Monthly Payment: $"<< mortgage(c, b, a) << endl;

cout << " \n";

cout << "You Entered" << endl;
cout << "Loan Amount: $" << a << endl;
cout << "Loan Rate: " << b << "%" << endl;
cout << "Loan Term: " << c << "years" << endl;
cout << "Monthly Payment: $" << mortgage (c, b, a) << endl;


return0;
}
 
  • #84
Is there a version of the getSubString function in c++? If so, what is it and what are the parameters?

-Thanks in advance
 
  • #85
alright, so I am an Electrical Engineering major, and my current project requires me to program...
im using Visual STudio 2005, and I am having a little trouble with just the basics...

here's my program so far:

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

int main
{
printf("heuy");
cout<< "hey";


return 0;

}

i run it, and all it gives me is a blank window...
it doesn't write anything...
can anyone help me?
 
  • #86
mmm ...did tha tcode actually compile?
my guess would be that your main call is missing something.

if it closes to fast put a pause statement like delay, getch, getchar.
 
  • #87
hiiiiiii(mamboooooooooooooooooo)

I hope some of you will not understand may be the language i used in the brackets but are the swahili word so don't worry about it but what i do want to ask from your my friends is just the sammary of c programming language because am still learner of that language but also u can ask me about pascal programming language because somehow iam well on it i hope you will accept my request
 
  • #88
hi my friends
i am a new in this side and i wana your help as fast as you can pleaaaaaaaas>>

the "q" is how to write the function in c++ if
x1=6 ,f(x)=x^2-4 and f'(x)=2x by using Newton raphson method
and the next estimate of the root:
xk+1 = xk - f(xk) / f'(xk)

and the answer should be if i enter the (0.1):

- Enter precision (1/10, 1/100, 1/1000,...): 0.1

xk f(x) derived_f(xk) xk+1 dx
6.000 32.000 12.000 3.333 2.667
3.333 7.111 6.667 2.267 1.067
2.267 1.138 4.533 2.016 0.251
2.016 0.063 4.031 2.000 0.016


pleas don't be late>>>>>thanks
 
  • #89
which is better:
using c++ (graphics mode) or using Visual c++??
i'm sort of confused.
 
  • #90
Nuclear: graphics mode of what? opengl, directx, graphics.h??
 
  • #91
how viable would it be if we had a sticky on a pointers tutorial? Or a quick refresher - i always end up linking to wikipedia then a couple of college sites when explaining pointers over the net on forums (mostly code comments, cramster etc).

it'd be awesome if we can get a collaborative effort on making the best pointers tutorial on the net ...

if it sounds to geeky stop me right now ...
 
  • #92
Help with assignment 4

Help with assignment 4
Im trying to input a number with at least three digits:
heres what i did

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

int main()
{
int number;

cout << "Enter a number:";
cin >> number;
cin.ignore();


if (number == setprecision(3) )
{
cout << "Number has 3 digits." << endl;
}
else
{
cout << "It is not a 3 digit number." << endl;
}
cin.get();
return 0;
somethings wrong and i know its the number == setprecision(3) what should i do?

by the way i just finished number 2 in assignment 4
please check and is there any other shortcut method ?
#include <iostream>
using namespace std;

int main()
{
char character;

cout << "Enter any letter from A to Q: ";
cin>>character;
cin.ignore();

switch (character) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
cout << character;
break;
default:
cout << "Letter not in the list. " << endl;

}
cin.get();
return 0;
}
 
Last edited:
  • #93
Pascal's triangle

dduardo's Assaignment # 5
Hey i need help with this one..
kindly explain with detail?

i also found this on another forum
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int pascal(int row, int col)
{
if (row == 0 || col == 0 || row == col + 1)
return 1;

return pascal(row - 1, col - 1) + pascal(row - 1, col);
}

int main(void)
{
int n;
cout << "Enter Row: ";
cin >> n;
cin.ignore();
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < i; ++j)
cout << pascal(i, j) << " ";
cout << endl;
}
cin.get();
return 0;
}
they are using the code pascal which is not explained in dduardo's tutorial
 
  • #94
I briefly looked over the C++ tutorial, and it looked fairly good. One thing though - maybe you should mention the Code::Blocks IDE (http://www.codeblocks.org), and that if one decides to use it, they should use the latest Nightly Build. Although Dev-C++ is still pretty good, it is getting old, and Code::Blocks is becoming better and better than it.
 
  • #95
Equilibrium said:
by the way i just finished number 2 in assignment 4
please check and is there any other shortcut method ?
There is, since the requested range of letters is simply a range of ASCII codes.
 
  • #96
Equilibrium said:
dduardo's Assaignment # 5
Hey i need help with this one..
kindly explain with detail?

i also found this on another forum

they are using the code pascal which is not explained in dduardo's tutorial

"pascal" is a user-defined function.

Hmm...recursion in C/C++ is often inefficient and can lead to memory problems...
 
  • #97
hows this for the hypotenuse assignment?

Code:
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cmath>
using namespace std;

main()
{
      long double num1, num2, num3;
      int number, num4, reset = 1;
      do {
      cout << "1. Hypotenuse" << endl << "2. Other" << endl;
      cin >> number;
      switch (number) {
             case 1:
      cout << "Enter the two sides, each followed by the enter key" << endl;
      cin >> num1 >> num2;
      num2 = sqrt(num1*num1 + num2*num2);
      break;
      case 2:
           cout << "Please enter the hypotenuse" << endl;
           cin >> num1;
           cout << "Please enter the other side" << endl;
           cin >> num2;
           num3 = sqrt(num2*num2 - num1*num1);
           cout << num3 << endl;
           break;
           }
      num3 *= 1000;
      num1 = num3 - floor(num3);
      num1 *= 10;
      num1 = floor(num1);
      if (num1 >= 5) {
               num3 = ceil(num3);
      } else  {
               num3 = floor(num3);
              }
      num3 /= 1000;
      cout << "Thankyou, the desired length is: " <<  num3 << " to 3d.p." << endl;
      cout << "Would you like to reset? (1 to reset)" << endl;
      cin >> reset;
      } while ( reset == 1);
      return 0;
}

edit:
i have improved it, with menu system and other side calculation but I am having trouble getting a big enough variable to hold a number bigger than 9 as well as with 3 or more decimal places... it seems long double isn't enough...

what should i be using?

still have no idea about the 3 digit assignment...
 
Last edited:
  • #98
p.s. for the 3 digit assignment... can it just be eg "482" or should it include things like 4.82?

equilibrium.. the shorter way is to just say
Code:
if ((num2 > 'a' && num2 < 'q') || (num2 > 'A' && num2 < 'Q')) {
               cout << "yes\n";
               } else {
               cout << "no\n";
               }

and for pascal, i created my own functions and then fiddled to troubleshoot. i had the program automatically enter row 0 and then i set the first position of each row to 0...

but still i don't even know where to start on the 3 digit one...
 
  • #99
"assignment 1.cpp": W8057 Parameter 'argv' is never used in function main(int,char * *) at line 10??
 
  • #100
i didn't understand argv and argc?
 
  • #101
"assignment 1.cpp": W8057 Parameter 'argv' is never used in function main(int,char * *) at line 10
 
  • #102
I need help on two questions...
1)Is C different from C++?If yes then in what ways?

2)Can someone please show me a link to download C/C++ for free??
 
  • #103
saket1991:

1. Yes, C is different from C++. C is pretty much (but not completely) a subset of C++. C++ is basically C with object-oriented features.

2. You don't really download C/C++. You can download a C/C++ compiler, linker, and editor, or an IDE (integrated development environment - pretty much combines the compiling, linking, and editing processes) . If you're using windows, you might want to try Dev-C++ or Code::Blocks (if you choose to use Code::Blocks, I recommend getting the latest Nightly build - read the stickied threads at the link for more information on using Nightly builds). Dev-C++ and Code::Blocks are both IDEs, by the way.
 
Last edited:
  • #104
First off, dduardo, thank you for what has been, so far, a most excellent tutorial.

Having said that, the pascals triangle task has seemed daunting to me, and I had to look up how to make my own functions to do it efficiently, and I came up with this:

Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

long double factorial (int num)
{
 if (num==1)
  return 1;
 return factorial(num-1)*num; // recursive call
}

int pscl (int n, int r)
{
    int out;
    long double nf=factorial(n), rf=factorial(r), nrf=factorial(n-r);

    long double btm=rf * nrf;

    long double tout=nf/btm;

    out = (int)tout;

    return out;
}

int main(int argc, char *argv[])
{
    int rin;
    int nin;
    int pout;
    
    cout << "input row: ";
    cin >> rin;

    cout << "input number: ";
    cin >> nin;

    pout=pscl(nin, rin);
    cout << pout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

the function 'factorial' is fairly obvious, and is a double float because factorials can be big. 'pascal' uses the binomial coefficient thing to calculate the number at a given place and row (n, r).

but this code crashes, and the debugger (when it decides to work) tells me it gets an 'access violation'. I tried breakpoints, but as I said, the debugger is a bit temperamental. Can you see anything wrong with my code?
 
  • #105
No idea what the original question were, but I can tell you your program is crashing because / when the end user is entering a number for 'rin' that's larger than 'nin'. I.e. when 'n-r' < 0. (It might also crash when n-r == 0, but too lazy to try.)
 
Back
Top