I'm trying to figure out how to turn ints into pointers but every-time I try, the program errors out. So I have to revert back to using regular int i.Keep in mind it compiles fine now. Anyway, whats the best way to go about turning "int i" or any "int" for that matter in all of my functions to pointers and then making the calculations work. The int get.cpp function below is the only one I think that works successfully but it sill confuses me when I try to imitate it on other functions. The functions are all in separate files so that's why it looks split like this.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
usingnamespace std;
constint N = 100;
int get(int * a, int max);
void put(int * a, int len);
bool isinorder(int * a, int len);
void selection(int * a, int len);
int findmax(int * a, int len);
void swap(int *, int *);
(next function)
1 2 3 4 5 6 7 8 9 10 11 12 13
// returns to the highest element position in a[0] - a[len]
int findmax(int * a, int len)
{
int current = 0;
int i = 1;
while (i <= len)
{
if (*(a + i) > *(a + current)) current = i;
i++;
}
return current;
}
(next function)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Get keyboard up to max ints, return the number typed in.
int get(int * a, int max)
{
cout << "Type some ints: " << endl;
//Below, points to first array element
int *p = a;
while (cin >> *a)
{
a++;
if (a - p == max) break;
}
return a - p;
}
(next function)
1 2 3 4 5 6 7 8 9 10 11
bool isinorder(int * a, int len)
{
int i = 0;
while (i < len - 1)
{
if (*a > *(a + i)) returnfalse;
a++;
i++;
}
returntrue;
}
#include "ar.h"
int main()
{
int * a;
a = newint[N]; // Get N new intergers during runtime.
int len = get(a,N);
cout << "Elements in original order: " << endl;
put(a, len);
cout << endl;
if (isinorder(a,len)) cout << "in order!" << endl;
else
{
selection(a,len);
cout << "Elements in sorted order: " << endl;
put(a, len);
cout << endl;
}
return 0;
}
(next function)
1 2 3 4 5 6 7 8 9 10 11 12
#include "ar.h"
void put(int * a, int len)
{
int i = 0;
cout << endl;
while (i < len)
{
cout << *(a + i) << endl;
i++;
}
}
(next function)
1 2 3 4 5 6 7 8 9 10 11
#include "ar.h"
void selection(int * a, int len)
{
while (len--)
{
// j points to biggest elements in array a
int j = findmax(a, len);
swap( a + j, a + len); //Same as swap(&a[j], &a[len]);
}
}
(last function)
1 2 3 4 5 6
void swap(int * x, int * y)
{
int temp = *x;
*x = *y;
*y = temp;
}
Below is the output I get now (which is correct). I want to get the same exact output but I need to change as many "ints" as I can into pointers. I am using a file full of numbers to compile. Thats why the numbers are random.
!g++ *.cpp && a.out < /tmp/input
Type some ints:
Elements in original order:
What I want to do is whats in the "get" function below. The function below used to have "int i = 0" and the calculations would be different in relation to i. But I changed the ints into pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int get(int * a, int max)
{
cout << "Type some ints: " << endl;
//Below, points to first array element
int *p = a;
while (cin >> *a)
{
a++;
if (a - p == max) break;
}
return a - p;
}
Now how could I use the same style of using pointers for int i for the example below for instance. Notice that in the "if" line I already have pointers and it works fine. All of this is part of a project I need to do. I wouldnt typically try this either but I must.
1 2 3 4 5 6 7 8 9 10 11
bool isinorder(int * a, int len)
{
int i = 0;
while (i < len - 1)
{
if (*a > *(a + i)) returnfalse;
a++;
i++;
}
returntrue;
}
I tried "int *p = a. and fooled around with the rest but it wont work.
Im trying to turn all of the "int i = 0" or int i = 1" into pointers.
I know its something to the effect of "int * p = a" but changing the rest of the function below the decleration is what gives me problems.
ne555, I think you’re on the right track with what you did in bool isinorder. Once I can get my ssh secure shell client working again I will work with that and see how it comes out.
I don’t personally want to make these into all pointers. It’s part of a bigger project we are doing in class.
As for compiling, we pull a file that has a list of numbers. We just have the size set to 100 even though there is probably only 20 numbers in the file.
I think I get it
You don't want to use a counter, but just pointers. ¿why?
1 2 3 4 5 6 7 8
bool isinorder(int * a, int len){
int *end = a+len;
while (a < end-1){
if (*a > *(a + 1)) returnfalse;
a++;
}
returntrue;
}
ne555. I think what you did here hit it spot on. This is what I'm looking for. I can easily change the int i into pointers. Its just changing the calculations below to work with it confuses me. Thanks.