#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11

    Need Help with C++ Program...


    Does anyone know how I could do this problem, right now im currently stressing on how to do this...Im getting errors left and right...~SIGH~ :o


    Given an array of type int that contains the score of students
    int score[NUM_STUDENTS];

    Write a function that takes score as a parameter and reverses the order of the components in score; that is, score [0] goes into score [NUM_STUDENTS 1], score [1] goes into
    score [NUM_STUDENTS 2], and so on.
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    What part of the problem are you having trouble with? One method would be simply to step through the array until you reach halfway, swapping elements from the beginning and end halves as you go along.

    Show us what code you've got and somebody will help you.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    mANNNN...i dont know wut im doing but here guys hope u guys can help me...im really brain dead...I dont think im even answering the question...haha...manN...
    -----------------------------------------------------------------------------------
    Code:
     
    
    #include<iostream>
    #include<string>
    using namespace std;
    
    void Print_Array(int[], int );
    
    int main()
    {
    	int score[Num_Students] = {1,2,3,4,5};
    	Print_Array(score, 5);
    
    	int score[100];
    
    	for( int i = 0; i < 100; i++)
    	{
    		cout<<"Enter Students Score"<<endl;
    		cin>>score[i];
    	}
    	for( i = 9; i >= 0; i--)
    	{
    		cout<<"The scores in the reverse direction are "<<endl;
    		cout<<score[i]<<endl;
    	}
    return 0;
    
    void Print_Array(int x[], int count)
    {
    	for(int i = 0; i < count; i++)
    		cout<<x[i]<<endl;
    	
    }
    Last edited by JusTHeRe; August 30th, 2003 at 01:13 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    can anyone help me with my problem...I need any help possible...thanks
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    > #include<iostream>
    > #include<string>
    > using namespace std;
    >
    > void Print_Array(int[], int );

    You haven't declared any other function here, but I thought you had to write a function to reverse the order of the array? C++ has a reverse() function that you could use, but clearly you're expected to write your own as an exercise, so:

    void reverseArray(int* intArray, int numberOfElements);

    > int main()
    > {
    > int score[Num_Students] = {1,2,3,4,5};
    OK, so that line declares your array, with a name 'score' and the number of elements is 'Num_Students'. Unfortunately you haven't defined Num_Students anywhere so this is probably giving you a compilation error. Somewhere prior to the previous line you need something like:

    const int NUM_STUDENTS = 5;

    >
    > Print_Array(score, 5);
    It's safer to use the same constant here that you used to create the array, so better is:
    Print_Array(score, NUM_STUDENTS);


    >
    > int score[100];
    You're declaring an array with the name 'score' here, but you've already done this, in the same scope, so your compiler should be complaining about the redefinition. I'm not sure why you need a second array anyway, if the user inputs values as shown below, why do you need the first array you declared? If you need two arrays, give them different names so that the compiler knows which is which.

    > for( int i = 0; i < 100; i++)
    > {
    > cout<<"Enter Students Score"<<endl;
    > cin>>score[i];
    > }
    > for( i = 9; i >= 0; i--)
    > {
    > cout<<"The scores in the reverse direction are "<<endl;
    >cout<<score[i]<<endl;
    > }
    > return 0;
    Your main() function just seems to end here, without a closing brace '}' so by now your compiler will probably be throwing up a frightening heap of errors and warnings. Horrible, isn't it, when it does that ;-) Not to worry though, just tackle them one at a time and recompile, you'll get through them.


    >void Print_Array(int x[], int count)
    >{
    > for(int i = 0; i < count; i++)
    > cout<<x[i]<<endl;
    > }
    As I noted earlier, you need to reverse the elements of your array.

    void reverseArray(int* intArray, int numberOfElements)
    {
    // have a think about the following (steps, not C++ code):
    create an integer (head) and set it to the first element (0)
    create another integer (tail) and set it to the last element (numberOfElements-1).
    loop while head is less than tail
    - swap element head with element tail
    - increment head
    - decrement tail
    end loop
    }

    By the time you get to the end of the loop, the elements should be reversed. Swapping elements should be straight forward. C++ has a swap() function, but once again I assume the exercise expects you to write you own? I assume you can fathom that, if not, ask.

    Does that help some?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    WOW...thanks BIGBADBOB...that helps alot, are you a TEACHER, cause man that makes sense...ill try and modify my program from your comments...thanks alot man...

    Oh yea, as u can tell from my problems...Im a beginner in writing C++ programs...this is my first class, I didnt expect it to be so complicated...man...but I can tell it just gets harder...
    Last edited by JusTHeRe; August 31st, 2003 at 01:35 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    hmm...i got the program to run but its not running the way I want it...as in its not answering my question...im gonna try to throughly read my textbook, too see if i can find examples that I can use to do this problem...I might have to recreate the program cause It doesnt look like its doing what I want it to do...

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    
    void reverseArray(int* intArray, int Num_Students); 
    void Print_Array(int[], int );
    
    int main()
    {
    	const int Num_Students = 5;
    	int score[Num_Students] = {1,2,3,4,5};
    	Print_Array(score, 5);
    
    	for( int i = 0; i < 100; i++)
    	{
    		cout<<"Enter Students Score"<<endl;
    		cin>>score[i];
    	}
    	for( i = 9; i >= 0; i--)
    	{
    		cout<<"The scores in the reverse direction are "<<endl;
    		cout<<score[i]<<endl;
    	}
    return 0;
    }
    
    void Print_Array(int x[], int count)
    {
    	for(int i = 0; i < count; i++)
    		cout<<x[i]<<endl;
    	
    }
  14. #8
  15. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    I would tend to start by writing your program in small stages and get each stage to work before I move onto the next. When you're a beginner as you are, even a dozen lines of code can seem like too much. (Don't worry, we all went through this at some time. It will begin to fall into place eventually).

    Maybe start by creating an array that is already populated with values and display them on the screen. Once you have that working successfully - compiled and running OK - then write your reverse function and get that working. Once that's done, then add the code to let the user input the values instead of having the array already populated.

    Divide and conquer. Code a little, test a little, fix a little, repeat until done.

    Good luck.

IMN logo majestic logo threadwatch logo seochat tools logo