Thread: One more program help?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    One more program help?

    Hello if anyone could help me with this code, I would appreiciate. It asks for us to use CMDline arguments and swap the elements of an array in reverse order. for example (2 3 4 5 6) is (6 5 4 3 2).
    Here is my code.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    int reverse(int numpts, int array[])
    {
         int temp;
    for (int i=0; i < numpts/2; i++) 
        {
            temp = array[i];
            array[i] = array[numpts-i-1];
            array[numpts-i-1] = temp;
        }
        return temp;
    }
    int main(int argc, char *argv[])
    {
      int npts = argc - 1;
      int x[50];
      for (int i = 1; i < argc; i++)
      {
      x[i-1] = atoi(argv[1]);
     cout << argv[i] << endl;
     } 
      cout << reverse (npts, x) << endl; 
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, you have a good start but a few subtle errors.

    Code:
    x[i-1] = atoi(argv[1]);
    You probably just set it to argv[1] for debugging purposes and never changed it back to i ( the loop variable ). This will give you the correct cmd line inputs. I don't know why you return a single integer from reverse? I do see that you fill in the reversed array into the original array that the caller passes in. This is fine but don't output the return value of reverse. Instead, run reverse then after that output the new values in the x array.

    Have fun.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    thanks, i think im still stuck though im still fresh in programming?
    is this any better? thanks.


    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    int reverse(int numpts, int array[])
    {
         int temp;
    for (int i=0; i < numpts/2; i++) 
        {
            temp = array[i];
            array[i] = array[numpts-i-1];
            array[numpts-i-1] = temp;
        }
        return temp;
    }
    int main(int argc, char *argv[])
    {
      int npts = argc - 1;
      int x[50];
      for (int i = 1; i < argc; i++)
      {
      x[i-1] = atoi(argv[i]);
     cout << argv[i] << endl;
     x[i-1] = reverse (npts, x);
     cout << x[i-1] << endl; 
     
     } 
      system("PAUSE");	
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    how is this any better?

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    void reverse(int numpts, int array[])
    {
         int temp;
    for (int i=0; i < numpts/2; i++) 
        {
            temp = array[i];
            array[i] = array[numpts-i-1];
            array[numpts-i-1] = temp;
        }
    }
    int main(int argc, char *argv[])
    {
      int npts = argc - 1;
      int x[50];
      for (int i = 1; i < argc; i++)
      {
      x[i-1] = atoi(argv[i]);
     cout << argv[i] << endl;
     reverse (npts, x);
     cout << x << endl; 
     } 
      system("PAUSE");	
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Getting closer..just a few things to pay attention to..

    in your main function your first for loop has too much in it. You don't want to call reverse in the loop. Put the end brace to that first for loop just above the reverse functino call. So the last line in the for loop should be the cout. Then when you want to display your reversed array do not simply put cout << x << endl; This will output the memory address of the array. You need to have another for loop to go through each element and display it. Just a simple something like:

    Code:
    for( int j = 1; j < argc; ++j )
    {
      cout << x[j - 1] << endl;
    }
    Will work fine..
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM