Author Topic: Another problem!!  (Read 18710 times)

0 Members and 2 Guests are viewing this topic.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Another problem!!
« on: October 16, 2006, 04:56:19 pm »
Can someone please help me?  I have been working on my programming assignment for my CS class and I have been running into a bunch of different problems.  Some of which I have been able to over come (I hope), and others I haven't.  One of these such problems is with the my_swap function that I created to help me sort an array.  When I sent the array to the my_swap function, I get a few errors.  At first, I have an infinite loop, but now I think I might have fixed that at the cost of getting two errors.  With my little knowledge of C++ I haven't been able to figure out what they mean.  So here they are:

Quote
hw6.cpp: In function `void my_swap(int*, int*)':
hw6.cpp:12: error: invalid conversion from `int*' to `int'
hw6.cpp:14: error: invalid conversion from `int' to `int*'
sortingalgs.cpp: In function `void bubbleSort(int*, int)':
sortingalgs.cpp:19: error: invalid conversion from `int' to `int*'
sortingalgs.cpp:19: error: invalid conversion from `int' to `int*'
sortingalgs.cpp: In function `void bobSort(int*, int)':
sortingalgs.cpp:42: error: invalid conversion from `int' to `int*'
sortingalgs.cpp:42: error: invalid conversion from `int' to `int*'

Now here's the code:

This is the file that contains the my_swap function:

Code: [Select]


#include "hw6.h"

void my_swap(int first[], int second[])
{
  int temp = 0;

 // cout<<"In hw6.cpp in the my_swap function";
  temp = first;
  first = second;
  second = temp;

  return;
}

The #include "hw6.h" is the header file where my prototype for the my_swap function is stored.

Now for the sortingalgs.cpp:

Code: [Select]
#include "sortingalgs.h"
#include "hw6.h"

void bubbleSort(int array[], const int SIZE)
{
 // cout<<"In sortingalgs.cpp in the bubbleSort";
  bool done = false, swapped = true;
  do
  {
    swapped = false;
    for(int index = 0; index <= SIZE - 1; index++)
    {
      if(array[index] > array[index + 1])
      {
       my_swap(array[index], array[index + 1], index);  //swaps the values of the new indexes (Line 19)
       swapped = true; //Everytime it swaps, this is true.
      }
    }
    if(swapped == false) //Once swapping is over, this is true, and the for loop ends.
      done = true; //ends do-while loop.
  }while(done == false);

  return;
}

void bobSort(int unsorted_sub[], const int SIZE)
{
  cout<<"In sortingalgs.cpp in the bobSort";
  int sorted_sub[] = {}; //declares an array with an undetermined size filled with 0's (wrong?)
  for(int index = 0; index <= SIZE - 1; index++)
  {
    //Assigns the sorted array all of the unsorted array, so that it can be sorted
    sorted_sub[index] = unsorted_sub[index];
    for(int index2 = 0; index2 <= index; index2++)
    {
      //Allows for two of the same arrays to exist so that the sorted can be sorted
      if(sorted_sub[index2] > sorted_sub[index])
        my_swap(sorted_sub[index2], sorted_sub[index], index2); //line 42
    }
  }
return;
}

I don't know what I am doing wrong. :(
« Last Edit: October 16, 2006, 05:08:43 pm by AntiVirus »
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #1 on: October 16, 2006, 05:14:05 pm »
Your swap function is switching around addresses of the first node in an external array.  Simply put, it's not doing anything for you!  If you want to swap two values in an array, you want to pass to the function the addresses of the nodes that you want to swap because you don't care about some internal array that's left to rot after the function is done!

You want it to look something like this:

Code: [Select]
void swap_values(int &value1, int &value2) // These arguments give the functions the ADDRESSES of these variables
{
   // Assign the value located at "the address of" value 1 to a temporary variable
  int temp = *value1;

  // Assign the value located at the address of value 1 to the value located at the address of value 2
  *value1 = *value2;

  // Assign the value of the temporary variable to the value located at the address of value 2
  *value2 = temp;
}

Then, when you use this function, it should look something like this:

Code: [Select]
swap_values(&array_1[2], &array_2[2]);
I'm not entirely sure that that's the correct syntax.  You may need to do something really tricky like (&array_1 + x), where x is the node you're trying to access.
« Last Edit: October 16, 2006, 05:16:17 pm by Sidoh »

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #2 on: October 16, 2006, 05:19:07 pm »
The first[] and second[] signify that it's an array I am passing in, and acts like a reference, I believe. 
« Last Edit: October 16, 2006, 05:21:33 pm by AntiVirus »
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #3 on: October 16, 2006, 05:25:34 pm »
try (int **value1, int **value2)

A pointer stores a memory address instead of an actual value.

Code: [Select]
int value = 2;

int *address = &value2;

*address = 3;

cout << value;

You'd get 3.

Here's some sample code:

Code: [Select]
sidoh@deepthought:~/public_html/src$ cat test.c; gcc test.c -o test; ./test
#include <stdio.h>

int main( int argc, char *argv[] ) {

        int value = 2;

        int *address = &value;

        *address = 3;

        printf("%d -> %d\n", address, value);

}
-1073744140 -> 3

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #4 on: October 16, 2006, 05:33:36 pm »
Code: [Select]
sidoh@deepthought:~/public_html/src$ cat test.c; gcc test.c -o test; ./test
#include <stdio.h>

void swap();

int main( int argc, char *argv[] ) {

        int array1[3] = {1, 2 ,3};
        int array2[3] = {3, 2, 1};

        printf("\n\n");

        printf("Array 1, node 0 has a value of %d.\nArray 2, node 0 has a value of %d.", array1[0], array2[0]);

        swap(&array1[0], &array2[0]);

        printf("\n\n");
        printf("Array 1, node 0 has a value of %d.\nArray 2, node 0 has a value of %d.", array1[0], array2[0]);

        printf("\n\n");

        return 0;

}

void swap(int **value1, int **value2)
{
        int temp = (int)*value2;
        *value2 = *value1;
        *value1 = temp;

        return;
}
test.c: In function `swap':
test.c:29: warning: assignment makes pointer from integer without a cast


Array 1, node 0 has a value of 1.
Array 2, node 0 has a value of 3.

Array 1, node 0 has a value of 3.
Array 2, node 0 has a value of 1.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #5 on: October 16, 2006, 05:38:33 pm »
What is **?  Two pointers?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #6 on: October 16, 2006, 05:41:38 pm »
What is **?  Two pointers?

Those confused the hell out of me when I had to use them.  It's the address of a pointer variable, so basically, yeah.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #7 on: October 16, 2006, 05:57:40 pm »
Eh.....

Well you would think that since I haven't learned this stuff I wouldn't be required to use them in my assignment.  So there has to be a way around this without using the pointer operator/function(?).

But I can't figure it out!!!!!!! :(
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #8 on: October 16, 2006, 06:17:36 pm »
Then don't use a function!  I was asking you why you were doing that in the first place yesterday with the other sort.

Just do something like:

Code: [Select]
int temp = 0;

while(...)
{
   ...
   temp = array1[index];
   array1[index] = array2[index];
   array2[index] = temp;
   ...
}

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #9 on: October 16, 2006, 06:43:16 pm »
[Edit]!!!

Actually, it just looked like an infinte loop, but instead it says Segmentation fault.  Whatever that is.
« Last Edit: October 16, 2006, 06:48:04 pm by AntiVirus »
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #10 on: October 16, 2006, 06:52:34 pm »
That doesn't make any sense.

Code: [Select]
sidoh@deepthought:~/public_html/src$ cat test.c; gcc test.c -o test; ./test
#include <stdio.h>

void swap();

int main( int argc, char *argv[] ) {

        int array1[3] = {1, 2 ,3};
        int array2[3] = {3, 2, 1};

        printf("\n\n");

        printf("Array 1, node 0 has a value of %d.\nArray 2, node 0 has a value of %d.", array1[0], array2[0]);

        //swap(&array1[0], &array2[0]);
        int temp = array1[0];
        array1[0] = array2[0];
        array2[0] = temp;

        printf("\n\n");
        printf("Array 1, node 0 has a value of %d.\nArray 2, node 0 has a value of %d.", array1[0], array2[0]);

        swap(&array1[0], &array2[0]);

        printf("\n\n");
        printf("Array 1, node 0 has a value of %d.\nArray 2, node 0 has a value of %d.", array1[0], array2[0]);

        printf("\n\n");

        return 0;

}

void swap(int **value1, int **value2)
{
        int temp = (int)*value2;
        *value2 = *value1;
        *value1 = temp;

        return;
}
test.c: In function `swap':
test.c:37: warning: assignment makes pointer from integer without a cast


Array 1, node 0 has a value of 1.
Array 2, node 0 has a value of 3.

Array 1, node 0 has a value of 3.
Array 2, node 0 has a value of 1.

Array 1, node 0 has a value of 1.
Array 2, node 0 has a value of 3.

PM me your code and I'll try to take a look at it.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #11 on: October 16, 2006, 06:54:26 pm »
Well I figured out what a Segmentation Fault is.

Quote
A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use.
But I don't see where I am doing that?

And okay, I will PM you my code.
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Another problem!!
« Reply #12 on: October 16, 2006, 06:57:07 pm »
Well I figured out what a Segmentation Fault is.

Quote
A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use.
But I don't see where I am doing that?

Hehe, yep. :)

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Another problem!!
« Reply #13 on: October 16, 2006, 07:06:02 pm »
Could it be because of the array[index] = array[index + 1];      ?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Another problem!!
« Reply #14 on: October 16, 2006, 07:18:04 pm »
[Edit]!!!

Actually, it just looked like an infinte loop, but instead it says Segmentation fault.  Whatever that is.

If you're infinitely looping you're bound to access some restricted memory sooner or later.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling