Thread: Adding pointers

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Adding pointers

    I have a pretty big program that I need to put into pointer notation. I really don't even know where to start. The program has 4 functions. After I learn how to do the first function, I'm sure I'll be able to do the rest on my own, I just need help starting it.


    Code:
    int main()
    {
    void sort( int [], int );
    
    int i, score[25], n = 0;
    
    // Input
    cout << "Enter first score: ";
    cin >> score[n];
    while( score[n] != -999 ) 
    {
    n++;
    cout << "Enter Score (-999 to end): ";
    cin >> score[n];
    }
    
    
    // Sorted Scores
    sort( score, (n-1) );
    cout << "Sorted scores:\n";
    for(i=0; i<n; i++)
    {
    cout << score[i];
    if(i<(n-1)) cout << ", ";	
    cout << endl << endl;
    }
    
    
    void sort( int a[], int n ) // Function header
    {
    int i, j, temp;
    
    for(i=0; i<n; i++) 
    {
      for(j=(i+1); j<=n; j++) 
      {
        if( a[j] > a[i] ) 
        {
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
      }
     }
    }
    
    return;
    }

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    name[index] == *((type*)name + index)

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I just got it working properly. Thanks. I'll post if I need help on any other of the functions.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    void sort( int [], int );
    
    int i, score[25], n = 0;
    
    // Input
    cout << "Enter first score: ";
    cin >> score[n];
    while( score[n] != -999 ) 
    {
    n++;
    cout << "Enter Score (-999 to end): ";
    cin >> score[n];
    }
    
    
    // Sorted Scores
    sort( score, (n-1) );
    cout << "Sorted scores:\n";
    for(i=0; i<n; i++)
    {
    cout << score[i];
    if(i<(n-1)) cout << ", ";	
    cout << endl << endl;
    }
    
    system("pause");
    
    return 0;
    }
    
    
    void sort( int a[], int n ) // Function header
    {
    int i, j, temp;
    int *ptr;
    
    ptr=a;
    
    for(i=0; i<n; i++) 
    {
      for(j=(i+1); j<=n; j++) 
      {
        if(*(ptr+j) > *(ptr+i )) 
        {
         temp = *(ptr+i);
         *(ptr+i) = *(ptr+j);
         *(ptr+j) = temp;
      }
     }
    }
    
    return;
    }
    Last edited by nick753; 10-22-2010 at 04:15 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by User Name: View Post
    Code:
    name[index] == *((type*)name + index)
    The (type *) [also known as a cast or more properly as an "explicit conversion"] is unnecessary in this.

    In this case, the problem is converting code from using "array notation" to "pointer notation". If an explicit conversion is necessary to achieve that, then it's a sign of a huge mistake in the conversion.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Alright, I've become stuck on the last function that I have to convert. It's a function to calculate the median. My book keeps showing and talking about pointers within a loop. Since this is not in any sort of loop, I don't really know the proper syntax or way of going about it. Thanks!


    Code:
    float median( int grade[], int n ) // Function header
    {
    	int *ptr;
    	ptr=grade;
    	float median;
    	int a, b;
    
    	a = n / 2;
    	b = a + 1;
    
    	if(n%2 ==0) //even
    	{
    		median = ((grade[a - 1] + grade[b - 1])) / 2.0;
    	}
    	else //odd
    	{
    		median = grade[a];
    	}
    	return median;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What exactly is the problem?
    Also, your previous code is in need of indentation.
    And your book seems to be a C book. Suggest you switch.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by Elysia View Post
    And your book seems to be a C book. Suggest you switch.
    A C book that teaches the use of cout?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A C book in sense that it does things like this:

    >> int *ptr;
    >> ptr=grade;

    >> int a, b;
    >> a = n / 2;
    >> b = a + 1;

    Typical C, or someone stuck in the past.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    My book is not a C book. The code of the median function wasn't from a book. Myself and someone on here came up with that code. The problem was stated in my previous post.

    My book keeps showing and talking about pointers within a loop. Since this is not in any sort of loop, I don't really know the proper syntax or way of going about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  3. Adding arrays with pointers - help
    By chip.litch in forum C Programming
    Replies: 2
    Last Post: 10-02-2008, 11:07 PM
  4. adding pointers?
    By ayoros in forum C Programming
    Replies: 4
    Last Post: 06-23-2007, 04:37 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM