math pointers

Post any maths and/or physics related questions here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

math pointers

Postby Mr. T » Thu Nov 13, 2008 12:55 pm

--------------------------------------------------------------------------------

Hey y'all,

I study math in France, and i need to program my first half year, and after that i am finished with c++. My first three assignments went very well, only the last one is a problem, hope you can help.

I need to make a program that asks a number, and than, by the use of pointers, i need to see if the inverse of the number is the same as the number itself. Like :

12345 --> 1 , 2 , 3 , 4 , 5 is not the same as 5 , 4 , 3 , 2 , 1.
When it is not the same, i have to give the sum of the two numbers, so 12345 + 54321, and then see if that number is inversible. It becomes difficult when 9, 8, 7 + 7, 8, 9 = 16, 16, 16 = 1776.

Thus, i managed to make an program with all kinds of voids, which should be no longer than 20 lines. This program uses pointers, which i must use, to seperate all the numbers.

I don't know how to inverse this, or how to get the sum.
The things i have learned, and may use, are classes, voids, pointers, arrays, and the basic features.

Can anybody help?

With much appriciation,

Thierry
Mr. T
 
Posts: 3
Joined: Thu Nov 13, 2008 12:53 pm

Postby Mr. T » Thu Nov 13, 2008 12:55 pm

this is what i got so far :


#include <iostream>
using namespace std;


class vak {
public:
int info;
vak* next;
vak* prev;

}; // vak


class gg {
public:
void Show ();
vak* in;
vak* out;


};

void Show (vak* in) {

vak* p = in;
while ( p != NULL ) {
cout << p->info << endl;
p = p->next;
} // while
cout << endl;
}
void Show2 (vak* out) {

vak* p = out;
while ( p != NULL ) {
cout << p->info << endl;
p = p->prev;
} // while
cout << endl;
} // Show


void Add (vak*& in, vak*& out, int number ) {

vak* p;
p = new vak;
p->info = number;
p->next = in;
in = p;
} // Toevoegen


void Type (vak* & in, vak* & out, int & number, int & i) {

cout << "Please give me a number: ";
char kar;
kar = cin.get ( );
while ( kar != '\n' ) {
if ( '0' <= kar && kar <= '9' )
Add ( in, out, kar-'0' );
kar = cin.get ( );
i++;
}
} // Type


int main ( ) {

vak* in = NULL;
vak* out = NULL;
int number;
int i = 0;

Type (in, out, number, i);
Show(in);
Show2(out);
cout << i << endl;

return 0;

} // main
Mr. T
 
Posts: 3
Joined: Thu Nov 13, 2008 12:53 pm

Postby ventsyv » Thu Nov 13, 2008 1:17 pm

You don't have to invert, you just have to check if the number is invertible right ?? Lets look at some "invertible" numbers:

121 12321 1221

What we see is that they are symmetrical. The first digit is equal to the last one, the second digit is equal to the second to last digit etc.

So if you start at both ends, you can compare digits and move toward the center. If all digits match, you have reversible number.

The addition is slightly more difficult. After you add member wise, you could end up, as you said, with 16,16,16. That's not a valid representation, since each "cell" is supposed to hold only one digit.

Here, you should think about what the position of each "cell" represents; namely the position of that digit in the number. You could then calculate the result (16,16,16) = 1776 and build a new list.

If you want to be elegant about it, you can overwrite the + operator.
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA

Postby Mr. T » Thu Nov 13, 2008 1:21 pm

yes, thank you for your response.

But, how do i start at both ends like you say? If i give a number in my original program, it goes only one way.

I try to see how the pointers work,

in -> info -> next = in etc

but how do i get this vica versa, with the prev and the out?
Mr. T
 
Posts: 3
Joined: Thu Nov 13, 2008 12:53 pm

Postby ventsyv » Thu Nov 13, 2008 3:44 pm

When you program, you should write one function at a time, make sure it works and only then write another one. That way if there are problems you know they are in the last few lines of code.
Here is what I'll do if I were you:
1. In main() read a number
2. Convert that number to a linked list representation
3. Print out the number (to make sure it was converted correctly)
4. Find if the number is invertible
5. Add two numbers together
6. Print out the result (again to make sure the operation was correct)

You need to get to step 3 before we can talk about the rest, but to answer your question:

You have two pointers in each node, one that points to the next item and one points to the previous item.
Getting the last item is easy:
Code: Select all
vak* GetLastItem(vak* start)
{
   while (*start->next != null)//the end is reached
   { start = start->next;}
    return start;
}

To go backwards (from right to left) you can do right = right->prev;
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA


Return to Maths & Physics

Who is online

Users browsing this forum: No registered users and 0 guests