Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code is from one of my classes. It's purpose is to take fill in a basket of fruits. There are 7 to choose from

This code is from one of my classes. It's purpose is to take fill in a basket of fruits. There are 7 to choose from but only combinations of 3 are allowed. My questions is, how do I make it so fruits can be inputed by a user rather than having a set assortment of fruits?

#include #include #include

#define NUM_FRUITS 7 #define BUY_AMOUNT 3

int count;

void comb(char ** fruits, int * current, int curfruit, int buyIndex);

int main () { int * cur; //which indexes have been selected char ** frNames; //The set of fruit names int i;

frNames = (char**) calloc(NUM_FRUITS, sizeof(char*)); frNames [0] =(char*) calloc(strlen("apples"), sizeof(char)); strcpy(frNames[0], "apples"); frNames [1] =(char*) calloc(strlen("grapes"), sizeof(char)); strcpy(frNames[1], "grapes"); frNames [2]= (char*) calloc(strlen("oranges"), sizeof(char)); strcpy(frNames[2], "oranges"); frNames [3]= (char*) calloc(strlen("grapefruit"), sizeof(char)); strcpy(frNames[3], "grapefruit"); frNames [4] =(char*) calloc(strlen("kiwi"), sizeof(char)); strcpy(frNames[4], "kiwi"); frNames [5] =(char*) calloc(strlen("tomatoes"), sizeof(char)); strcpy(frNames[5], "tomatoes"); frNames [6] =(char*) calloc(strlen("bananas"), sizeof(char)); strcpy(frNames[6], "bananas"); cur = (int *) calloc(BUY_AMOUNT, sizeof(int));

//fill the arrays; intialize with an integer -1, then it will be updated by each fruit id we select for( i = 0; i < BUY_AMOUNT; i++) { cur[i] = -1; } comb(frNames, cur, NUM_FRUITS, BUY_AMOUNT); return EXIT_SUCCESS; }

void comb( char ** fruits, int * current, int curFruit, int buyIndex) { int i;

if(buyIndex == 0) { printf("%d. ", ++count); for(i = BUY_AMOUNT -1; i >=0 ;i--) { printf("%s ", fruits[current[i]]); } return; } if (curFruit == 0) { return; }

current[buyIndex-1] = NUM_FRUITS - curFruit; comb(fruits, current, curFruit - 1, buyIndex-1);

current[buyIndex-1] = -1; comb(fruits, current, curFruit -1 , buyIndex); return;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions