+ 1

Lucky Winners

Passing arrays to functions A local supermarket is running a promotion: each Nth customer will receive one item for free. Customers names are sequentially given as array of strings (see template). Write a function that receives the array of customers, its size, and the N number as arguments, and prints the names of the lucky customers each in a new line. Sample Input 3 Sample output Rayan Bruce Richard Mary Remember to pass the size of the array as argument to the function. #include <iostream> #include <string> using namespace std; //complete the function void winners(string arr[], int size, int n) { for (int i=0;i>size;i++) { } } int main() { string customers[] = {"Alice", "Bob", "Rayan", "Emma", "Ann", "Bruce", "Synthia", "Daniel", "Richard", "Sam", "Nick", "Mary", "Paul"}; //getting the lucky number as input int n; cin >> n; //call function winners(customers, 13, n); return 0; } I am unsure how to represent the variable n which defines the lucky number in my function.

27th Mar 2022, 6:21 PM
Brian
4 Respuestas
+ 1
void winners(string arr[], int size, int n) { for (int i=0; i < size; i++) { if((i + 1) % n == 0) cout << arr[i] << endl; } }
27th Mar 2022, 6:50 PM
rodwynnejones
rodwynnejones - avatar
+ 1
While (i <size-n){ i+=n; cout <<customers [i]<<endl; }
27th Mar 2022, 7:08 PM
Z3ro
0
Question is missing some details surely... But i guess, nth customer means multiple 3 customers, n, n*2, n*3... Like for 3 => 3,6,9... Customers and index starts from 3 so ith index value when i+1%3==0 is true. You have i>size infinite, use i<size
27th Mar 2022, 6:32 PM
Jayakrishna 🇮🇳
0
..or void winners(string arr[], int size, int n) { for (int i = n - 1; i < size; i+=n) { cout << arr[i] << endl; } } ... or void winners(string arr[], int size, int n) { for (int i = n; i < size; i+=n) { cout << arr[i - 1] << endl; } }
27th Mar 2022, 7:01 PM
rodwynnejones
rodwynnejones - avatar