[c++] Function trouble (beginner stuff) menu

Shout-Out

User Tag List

Results 1 to 3 of 3
  1. #1
    Golf's Avatar Private
    Reputation
    1
    Join Date
    Feb 2011
    Posts
    10
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [c++] Function trouble (beginner stuff)

    Write a program that uses the following functions:

    Fill_array() takes as arguments the name of an array of double values and an array
    size. It prompts the user to enter double values to be entered in the array. It ceases taking
    input when the array is full or when the user enters non-numeric input, and it
    returns the actual number of entries.

    Show_array() takes as arguments the name of an array of double values and an array
    size and displays the contents of the array.

    Reverse_array() takes as arguments the name of an array of double values and an
    array size and reverses the order of the values stored in the array.

    The program should use these functions to fill an array, show the array, reverse the array,
    show the array, reverse all but the first and last elements of the array, and then show the
    array.
    My question is where do I go from here? I figure I have to re-reverse the array to retrieve the original array, but how would I go about reversing all the integers in the original array except for the first and the last ones?If anyone could drop a hint or something that would be great.

    P.s. is there a value i could return from Reverse_array() other than temp? could I just put 1 and it would have no effect on the function of the program? Or would it be best to just declare the function as type void since no return value is actually needed? If "void" would be the best options, does this conserve space the most since the compiler has one less step to do?
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    int Fill_array(double myArr[], int);
    void Show_array(const double myArr[], int);
    int Reverse_array(double myArr[], int);
    
    int main()
    {
        cout << "How big would you like the array to be? ";
        int size;
        int num_entered = 0;
        cin >> size;
        double firstarray[size];
        num_entered = Fill_array(firstarray, size);
        cout << "You entered " << num_entered << " numbers into the array.\n\n";
        cout << "Showing the array...\n";
        Show_array(firstarray, num_entered);
        cout << "\nReversing the array...\n";
        Reverse_array(firstarray, num_entered);
        Show_array(firstarray, num_entered);
    
        return 0;
    }
    
    int Fill_array (double myArr[], int n)
    {
        int num_entered = 0;
        cout << "Please fill the array(Non-numeric input to quit): \n";
        for (int i = 0; i < n; i++)
        {
            cout << "Input # " << i+1 << ": ";
            if (cin >> myArr[i])
                ++num_entered;
            else
                return num_entered;
    
        }
        return n
        ;
    }
    
    void Show_array(const double myArr[], int n)
    {
        for(int i = 0; i < n; i++)
        {
            cout << "Value #" << i+1 << ": " << myArr[i] << endl;
        }
    }
    
    int Reverse_array(double myArr[], int n)
    {
        double temp;
        for (int a = 0; a < --n; a++)
        {
            temp = myArr[a];
            myArr[a] = myArr[n];
            myArr[n] = temp;
        }
        return temp;
    }
    Also I had the idea to call Reverse_array(firstarray + 1, num_entered +1);

    That solves the first/last variables being left as is but it doesn't solve the issue with the middle variables. They come out mumbo jumbo!

    [c++] Function trouble (beginner stuff)
  2. #2
    _duracell's Avatar Active Member
    Reputation
    43
    Join Date
    May 2007
    Posts
    126
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use (firstarray + 1, num_entered - 2)

    Code:
    #include <iostream>
    using namespace std;
    
    int Fill_array(double ar[], int);
    void Show_array(double ar[], int);
    void Reverse_array(double ar[], int);
    
    int const maxSize = 20;
    int realSize;
    
    int main()
    {
    	int num_entered = 0;
    	double firstarray[maxSize];
    	char x;
    
    	cout << "How big would you like the array to be? ";
    	cin >> realSize;
    	num_entered = Fill_array(firstarray, realSize);
    	cout << "You entered " << num_entered << " numbers into the array.\n\n";
    	cout << "Showing the array...\n";
    	Show_array(firstarray, num_entered);
    	cout << "\nReversing the array...\n";
    	Reverse_array(firstarray, num_entered);
    	Show_array(firstarray, num_entered);
    	cout << "\nReversing all but end values...\n"; 
    	Reverse_array(firstarray + 1, num_entered - 2); 
    	Show_array(firstarray, num_entered);
    
    	cin >> x;
    }
    
    int Fill_array (double ar[], int arSize)
    {
    	int n;
    	cout << "Please fill the array (Non-numeric input to quit): \n";
    	for (n = 0; n < arSize; n++)
    	{
    		cout << "Input # " << n+1 << ": ";
    		if (!(cin >> ar[n])) break;
    	}
    	return n;
    }
    
    void Show_array (double ar[], int arSize)
    {
    	for (int i=0; i < arSize; i++)
    		cout << "Element #" << i+1 << ": " << ar[i] << endl;
    }
    
    void Reverse_array (double ar[], int arSize)
    {
    	double temp;
    	int x, y;
    
    	for (x = 0, y = arSize - 1; x < y; x++, y--) 
    	{
    		temp = ar[x];
    		ar[x] = ar[y];	
    		ar[y] = temp;
    	}
    }

  3. #3
    Golf's Avatar Private
    Reputation
    1
    Join Date
    Feb 2011
    Posts
    10
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks a ton duracell, that solved it!

Similar Threads

  1. Trouble hooking function with Detours
    By Miivers in forum WoW Memory Editing
    Replies: 5
    Last Post: 01-03-2016, 04:52 AM
  2. Beginners MC Guide
    By Amedis in forum World of Warcraft Guides
    Replies: 7
    Last Post: 01-05-2007, 11:44 PM
  3. Beginners Guide (Durotar)
    By Amedis in forum World of Warcraft Guides
    Replies: 0
    Last Post: 06-19-2006, 04:25 PM
  4. Beginners Guide (Tirisfal Glades)
    By Amedis in forum World of Warcraft Guides
    Replies: 0
    Last Post: 06-19-2006, 04:24 PM
  5. Free Stuff here
    By karokekid in forum Community Chat
    Replies: 6
    Last Post: 06-15-2006, 11:20 AM
All times are GMT -5. The time now is 04:47 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search