Thread: Problem with Pointers

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Problem with Pointers

    Hi everyone,

    I have a program where I need to print the values of an array of float values. Then I must sort the array and print the sorted values and THEN print the original values again.

    I am able to get the originals and sorted values printed but I am having trouble printing out the originals again after they are sorted. Instead I am getting the sorted values being printed out twice instead of having the originals being printed, then the sorted ones and then the original values again. The trick is the original values must be sorted BEFORE the original values are printed for the second time and the sorted version of the array has to be stored in an array of pointers to float which I think is what I have below and have it's own function.

    I am not allowed to modify the original array. How can I go about doing this so that the first the original values are printed, then the sorted values and then the original values are printed again?

    BTW, don't worry about the poor alignment of the output. I'll deal with that later. Right now, what is supposed to be the second printing of the originals is printing out as the sorted values which shouldn't be.

    Here is my code so far:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    
    int main() {
       void sort(float *, int);
       int j;
       const int num = 10;
       float x[num] = {2.3, 5.66, 1.22, 5, 6.777, 3.2, 5.13, 2.862, 1, 5.01};
    
       cout << "Original" << endl;
    
       for (j = 0; j < num; j++) {
          cout << x[j] << endl;
       }
       cout << endl;
    
       sort(x, num);
    
       cout << "Originals" << endl;
       for (j = 0; j < num; j++) {
          cout << x[j] << endl;
       }
    
       getche();
       return 0;
    }
    
    void sort(float *ptr, int n) {
    
       int j, k;
       float temp;
    
       for (j = 0; j < n - 1; j++) {
          for (k = j + 1; k < n; k++) {
             if (ptr[j] > ptr[k]) {
                temp = ptr[j];
                ptr[j] = ptr[k];
                ptr[k] = temp;
             }
          }
       }
    
       cout << "Sorted" << endl;
    
       for (j = 0; j < n; j++) {
          cout << ptr[j] << endl;
       }
       cout << endl;
    }
    If anyone can help me figure out how I can get the originals printed to the screen AFTER the sorted values, that would be greatly appreciated. Thanks.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Can you do something like this? Where you store it in a temp array, I think that would be best.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    using namespace std;
    
    int main() {
       void sort(float *, int);
       int j;
       const int num = 10;
       float x[num] = {2.3, 5.66, 1.22, 5, 6.777, 3.2, 5.13, 2.862, 1, 5.01};
    
       cout << "Original" << endl;
    
       for (j = 0; j < num; j++) {
          cout << x[j] << endl;
       }
       cout << endl;
    
       float temp[num];
       
       //here is where you will want to copy x array to temp array
          
       sort(x, num);
    
       cout << "Originals" << endl;
       for (j = 0; j < num; j++) {
          cout << temp[j] << endl;  //print the temp instead of x array
       }
    
       getche();
       return 0;
    }
    
    //you should find something better than bubble sort IMHO
    void sort(float *ptr, int n) {
    
       int j, k;
       float temp;
    
       for (j = 0; j < n - 1; j++) {
          for (k = j + 1; k < n; k++) {
             if (ptr[j] > ptr[k]) {
                temp = ptr[j];
                ptr[j] = ptr[k];
                ptr[k] = temp;
             }
          }
       }
    
       cout << "Sorted" << endl;
    
       for (j = 0; j < n; j++) {
          cout << ptr[j] << endl;
       }
       cout << endl;
    }

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Easiest Solution is to make a copy of x before you sort it...

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    
    using namespace std; 
    
    int main() {
       void sort(float *, int);
       int j;
       const int num = 10;
       float x[num] = {2.3, 5.66, 1.22, 5, 6.777, 3.2, 5.13, 2.862, 1, 5.01};
    
       cout << "Original" << endl;
    
       for (j = 0; j < num; j++) {
          cout << x[j] << endl;
       }
       cout << endl;
       float x2[num];
       x2 = x;
    
       sort(x, num);
    
       cout << "Originals" << endl;
       for (j = 0; j < num; j++) {
          cout << x2[j] << endl;
       }
    
       getche();
       return 0;
    }
    
    void sort(float *ptr, int n) {
    
       int j, k;
       float temp;
    
       for (j = 0; j < n - 1; j++) {
          for (k = j + 1; k < n; k++) {
             if (ptr[j] > ptr[k]) {
                temp = ptr[j];
                ptr[j] = ptr[k];
                ptr[k] = temp;
             }
          }
       }
    
       cout << "Sorted" << endl;
    
       for (j = 0; j < n; j++) {
          cout << ptr[j] << endl;
       }
       cout << endl;
    }

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    mrafcho001 this looks like an assignment and you shouldnt just give him the answer. Thread rules say dont give answers, but help.

    plus this:
    Code:
       x2 = x;
    is not standard c++
    your assigning the memory location to x2 so it will still be the same output as he had before

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by JoshR
    mrafcho001 this looks like an assignment and you shouldnt just give him the answer. Thread rules say dont give answers, but help.

    plus this:
    Code:
       x2 = x;
    is not standard c++
    your assigning the memory location to x2 so it will still be the same output as he had before
    Oh Im sorry..

    yeah i forgot it was arrays that needs to be copied

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    OK, the sorting works with that method and I'm pretty sure that is the only way it can be done. Is that true?

    Another question I have is that I need to integrate this program with the original and sorted values into a Windows BGI graphics mode program and one of the problems I'm having is trying to get it to display in the graphics mode.

    I know I need to use outtextxy() to output anything to the screen but it doesn't like float values and I need to find some way to convert the float data type of the values to char type values because that is the only data type that outtextxy() will recognize and print to the screen.

    How can I accomplish this?

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Pretty much any other way includes a temp array or something to store the original values.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    How can I convert a variable from a float to char? I know of atoi for char to int but I'm hoping there is one for float to char.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<sstream>
    std::string FtoS(float f)
    {
       std::ostringstream os;
       os<<f;
       return os.str();
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    Is there anyone here that is good with Windows BGI graphics within C++? I realize it's a terribly old standard, outdated and rarely used but I've been forced to in this case. I am trying to draw a red box around the largest of the original float values in my Windows BGI graphics mode program but I am coming across many problems with getting the box to appear in the correct position which is around the largest value is.

    Here is my code:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string>
    #include "c:\Dev-C++\Graphics\winbgim.h"
    
    int main() {
       void sort(float *, int);
       int j;
       int cnt = 0;
       const int num = 10;
       char tempstr[20];
       char highstr[20];
       float x[num] = {2.3, 5.66, 1.22, 5, 6.777, 3.2, 5.13, 2.862, 1, 5.01};
       float curhigh = 0.0;
       int driver;
       int mode;
       driver = DETECT;
       mode = 0;
       initgraph(&driver, &mode, "");
       setcolor(BLUE);
       settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
       outtextxy(12, 25, "Original");
       for (j = 0; j < num; j++) {
          cnt += 20;
          sprintf(tempstr, "%.2f", x[j]);
          outtextxy(12, 30 + cnt, tempstr);
          if (x[j] > curhigh) {
             curhigh = x[j];
    
          }
       }
       setcolor(RED);
       sprintf(highstr, "%.2f", curhigh);
       outtextxy(20, 300, "Highest Value");
       outtextxy(20, 320, highstr);
    
       float temp[num];
       temp = x;
    
       sort(x, num);
    
       cnt = 0;
    
       for (j = 0; j < num; j++) {
          cnt += 20;
          sprintf(tempstr, "%.2f", temp[j]);
          outtextxy(400, 30 + cnt, tempstr);
       }
    
       getch();
       return 0;
    }
    
    void sort(float *ptr, int n) {
    
       int j, k;
       int cnt = 0;
       char tempstr[20];
       float temp;
    
       for (j = 0; j < n - 1; j++) {
          for (k = j + 1; k < n; k++) {
             if (ptr[j] > ptr[k]) {
                temp = ptr[j];
                ptr[j] = ptr[k];
                ptr[k] = temp;
             }
          }
       }
       setcolor(RED);
       settextstyle(4, HORIZ_DIR, 2);
       outtextxy(310, 25, "Sorted");
    
       for (j = 0; j < n; j++) {
          cnt += 20;
          sprintf(tempstr, "%.2f", ptr[j]);
          outtextxy(250, 30 + cnt, tempstr);
       }
    }
    If anyone tries to run this it must have the following in compiler options: "-mwindows <path of the winbgim.cpp file>"

    I don't expect anyone to know a lot about Windows BGI environment because it is not common but I thought I'd give it a chance posting a problem on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM