1

There is a few lines of my code that I would like to define as a function because I plan to use it multiple times. portion of code is as follows:

// loop within loop used to reorder with highest price at the top
for(i=0;i<x;i++){
    for(t=i;t<x;t++){
        if(f[i].price < f[t].price) {
            temp = f[i].price;
            f[i].price = f[t].price;
            f[t].price = temp;
        }

    }
}

I hope to be able to enter new values for x and f each time I call the function. I have included all of my code below. If I'm unclear about my objective in anyway please feel free to ask. I apologize in advance for the improper terminology I am new to this. Thank you

#include <iostream>
using namespace std;

struct List
    {
        char name[10];
        int price;
        };

int main()
{
    //x represents number of structures within array!
     int x; 

    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;


    //array of structures fruit
    struct List f[x];

    int i;

    //input values into structure
    for (i = 0; i < x; i++) {
      cout << "\nEnter fruit name, and price.\n";
      cin >> f[i].name;
      cin >> f[i].price;
    };

    //variables for reordering
     int temp;
     int t;


    // loop within loop used to reorder with highest price at the top
    for(i=0;i<x;i++){
        for(t=i;t<x;t++){
            if(f[i].price < f[t].price) {
                temp = f[i].price;
                f[i].price = f[t].price;
                f[t].price = temp;
            }

        }
    }


    //Output of menus
    //fruit Menu
    cout << "\n\nFruit Menu";
     for (i = 0; i < x; i++) {
        cout << "\n" << f[i].name << " $" << f[i]. price;
        };


    return 0;
}
4
  • So what's your question? Commented Feb 13, 2017 at 7:19
  • 1
    Using std::sort() would be much better - Bubble Sort is almost never a good idea! Commented Feb 13, 2017 at 7:21
  • Hint: follow a rather old principle which says: "separate input from process from output". So write a function that gathers input, write a function for reordering, and then a function for printing the results. Combine them then in any way you like. Commented Feb 13, 2017 at 7:22
  • Use C++ standard containers Commented Feb 13, 2017 at 8:50

4 Answers 4

1

I suppose it is an assignment that says "implement your own sort function for sorting an array of fruits", so I take the data structure "array of fruits" as given. You can, of course, change this to vector<struct Fruit> as well, but that's a different topic.

Maybe the following fragments help you finishing your code. It contains functions for entering, sorting, and printing the array with some samples how to deal with the parameters and the calls. You'll have to finalise the code.

Have fun!

#include <iostream>
using namespace std;

struct Fruit
{
    char name[10];
    int price;
};

// enter up to nrOfFruis; return number of fruits actually entered
int enterFruits(struct Fruit *fruits, int maxNrOfFruits) {
    int entered = 0;
    while (entered < maxNrOfFruits) {

        cin >> fruits[entered].name;

        entered++;
    }
    return entered;
}

void sortFruits(struct Fruit* fruits, int nrOfFruits) {

    // your sort code goes here

    // example for swaping two elements:
    Fruit temp = fruits[0];
    fruits[0] = fruits[1];
    fruits[1] = temp;
}

void printFruits(struct Fruit *fruits, int nrOfFruits) {

    cout << "\n\nFruit Menu";
    for (int i = 0; i < nrOfFruits; i++) {
        cout << "\n" << fruits[i].name << " $" << fruits[i]. price;
    };
}



int main()
{
    // Your task: put a proper loop and exit condition arround the following lines...

    int x;
    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;

    struct Fruit fruits[x];
    int entered = enterFruits(fruits, x);
    sortFruits(fruits, entered);
    printFruits(fruits, entered);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot allocate an array on the stack if you do not know it's size at compile time. Therefore, you need to dynamically allocate memory for it(you also need to remember to delete it):

//x represents number of structures within array!
 int x; 

cout << "How many items would you like to list under the fruit menu?\n";
cin >> x;


//array of structures fruit
struct List * f = new List[x];

//...

delete [] f;

Alternatively, you could do it the C++ way, using vector, having the vector elements on the stack:

int x;
std::cin>>x;
std::vector<A> v(x);

for( size_t i = 0; i < x; i++)
{
    std::cin >> v[i].x;
}

Comments

0

If you just want to pass an array to a function you can do so like this:

void sortArray(struct List list[]);
void sortArray(struct List* list, int n); // n = size of array

may be better to just use std::vector or some other list container instead. :)

Comments

0

Sounds like you want a function that receives an array t and index x, and you want to mutate the array in the function?

C++ is "pass by value", so to mutate the array you have to have your function take a reference (or pointer) to the array so that you're mutating the original array and not a copy of it, so just have your function signature like this: func(T& t, int x) (assuming T is the type of array t).

2 Comments

The purpose of the function is to sort the structures within the array by price, which it does successfully. However I plan to use those few lines of sorting codes repeatedly as I want to sort other menus by price i.e Dairy, Beverage etc. Sorry my terminology is no good but x is the number of structures with the array. t is just a random variable I used for the sorting code and f refers to the array to be sorted. The values I would like to be able to change when calling the function are f and x so that the function can sort other arrays of structures with different quantities of structures.
Not sure if that made sense but I appreciate your effort.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.