0

passing arrays to functions - Who's the lucky winner?

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 No idea what I did wrong… almost done but solarlern app don’t show what I did wrong. #include <iostream> #include <string> using namespace std; //complete the function void winners(string costumers[],int no,int n) { int k=1; for (int i=0 ; i<=n ;i++ ) { if ( (n*k-1)<=13 && n>0){ cout<<costumers[(n*k)-1]<<endl; k++; } if (n<=0) cout<<0; } } 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; }

13th Nov 2020, 3:27 PM
Radek Skyradek Wlazlo
Radek Skyradek Wlazlo - avatar
24 Respuestas
+ 1
But... why? You already were really close, your solution was like 95% correct. All you had to do was to adjust the condition of the for loop to match the appropriate number of iterations necessary. I even gave you the value to compare 'i' to: 'no' / 'n'. Hardcoding is like practicing nothing at all.
14th Nov 2020, 12:23 PM
Shadow
Shadow - avatar
+ 9
#include <iostream> #include <string> using namespace std; //complete the function void winners(string costumers[],int size,int n) { int k=1; for (int i=0 ; i<=size ;i++ ) { if ( (n*k-1)<=13 && n!=0){ cout<<costumers[(n*k)-1]<<endl; k++; } if (n<=0) cout<<0; } } 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; }
14th May 2021, 2:55 PM
Darkwa John
Darkwa John - avatar
+ 4
Prathvi He's trying to solve the code coach problem described at the top, where the given code fails one test case. Radek Skyradek Wlazlo Iterating from 0 to 'n' might not be enough iterations to display all winners. Consider 2 as an example for 'n', where you would end up with 3 iterations in total, but 13 / 2 = 6 winners you need to display. You always need 'no' / 'n' iterations, to be precise.
13th Nov 2020, 3:58 PM
Shadow
Shadow - avatar
+ 4
that code work for me: #include <iostream> #include <string> using namespace std; //complete the function void winners(string arr[],int sz,int lck) { for(int i=0;i<=sz;i++){ if((i+1)%lck==0){ cout<<arr[i]<<endl; } } } 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; }
7th Jul 2021, 5:47 PM
David Cabezas
David Cabezas - avatar
+ 3
Ulu Mulu If you line up those customers, each nth person means the person at position n, next the person at position 2n, then the person at position 3n, and so on. The variable 'k' simply represents the multiplication factor, the amount of persons you go ahead by each iteration. Since arrays are zero-indexed, you subtract one from to the overall position. The condition simply checks there is still a person at the current position, and if that is the case, it is printed as a winner before moving to the next (incrementing k). By the way, the code you copied invokes undefined behaviour for input values n = 1, 2, 7. You should beware of correctness when copying other people's code.
15th Jul 2021, 1:29 PM
Shadow
Shadow - avatar
+ 1
for(int i=0; i<=no;i++) That is the source of error
14th May 2021, 2:55 PM
Darkwa John
Darkwa John - avatar
+ 1
#include<iostream> #include<string> using namespace std; void winners(string customers[],int size,int n) { int times=size/n; int interval=1*n; while(times>0&&n<=size){ cout<< customers[interval-1]<<endl; times--; interval+=n; } } 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; }
19th Jun 2021, 3:25 PM
zhenly
0
https://code.sololearn.com/cbT9r55Ul6Av/?ref=app Your code is working 👍 What's the problem?
13th Nov 2020, 3:44 PM
Prathvi
Prathvi - avatar
0
ohhhh, easy exercise and I make it so dificult. yes had to use all if if if n=0 n=1 and so on.... so easy Thank You Shadow !
13th Nov 2020, 11:09 PM
Radek Skyradek Wlazlo
Radek Skyradek Wlazlo - avatar
0
You hardcoded all possible values of 'n'?
13th Nov 2020, 11:23 PM
Shadow
Shadow - avatar
0
yes, I was tired to watch this problem and just simple write all possible values. i know that will be possible write taht a bit more sexi. at list devide for 4 grups but i wrote : { if (n<=0 || n>13) cout<<0; if (n>13) cout<<0; if (n==1) { cout<<costumers[0]<<costumers[1]<<costumers[2]<<costumers[3]<<costumers[4]<<costumers[5]<<costumers[6]<<costumers[7]<<costumers[8]<<costumers[9]<<costumers[10]<<costumers[11]<<costumers[12]<<endl; } if (n==2) { cout<<costumers[1]<<costumers[3]<<costumers[5]<<costumers[7]<<costumers[9]<<costumers[11]<<endl; } if (n==3) cout<<costumers[2]<<costumers[5]<<costumers[8]<<costumers[11]<<endl; if (n==4) cout<<costumers[3]<<costumers[6]<<costumers[10]<<endl; if (n==5) cout<<costumers[4]<<costumers[9]<<endl; if (n==6) cout<<costumers[5]<<costumers[11]<<endl; if (n==7) cout<<costumers[6]<<endl; if (n==8) cout<<costumers[7]<<endl; if (n==9) cout<<costumers[8]<<endl; if (n==10) cout<<costumers[9]<<endl; if (n==11) cout<<costumers[10]<<endl; if (n==12) cout<<costumers[11]<<endl; if (n==13) cout<<costumers[12]<<endl; }
14th Nov 2020, 10:21 AM
Radek Skyradek Wlazlo
Radek Skyradek Wlazlo - avatar
0
I'm still on this problem and I don't get how this code works
23rd May 2021, 2:30 AM
Tristan
0
#include <iostream> #include <string> using namespace std; //complete the function void winners(string costumers[],int size,int n) { int k=1; for (int i=0 ; i<=size ;i++ ) { if ( (n*k-1)<=13 && n!=0){ cout<<costumers[(n*k)-1]<<endl; k++; } if (n<=0) cout<<0; } } 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; } Hello, I wa my self on this task and got frustrated a bit. I found this solution but still don´t understand how it is working. Let me show you, where I get stucked: it´s this function: void winners(string costumers[],int size,int n) { int k=1; //why we using this variable? for (int i=0 ; i<=size ;i++ ) { if ( (n*k-1)<=13 && n!=0){// what excactly hapens here? cout<<costumers[(n*k)-1]<<endl; k++;// why k++? } if (n<=0) cout<<0; } } I know the questions are silly but still could some one explaine it to me? Thx
14th Jul 2021, 4:34 PM
Ulu Mulu
Ulu Mulu - avatar
0
My code can only solve test 2 and 4 #include <iostream> #include <string> using namespace std; //complete the function void winners( string customers[],int a,int n) { for (int x=0; x<=a; x+=n) { cout <<customers[(n-1)+x] <<endl; } } 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; }
31st Jul 2021, 10:01 AM
IIMarky1234 Nguyen
IIMarky1234 Nguyen - avatar
0
#include <iostream> #include <string> using namespace std; //complete the function void winners(string arr[],int sz,int lck) { for(int i=0;i<=sz;i++){ if((i+1)%lck==0){ cout<<arr[i]<<endl; } } } 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; } Good Luck
25th Jan 2022, 5:39 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
#include<iostream> #include<string> using namespace std; void customer_number(string customers[], int size, int n) { for (int i = 0; i < size; i++) { if((i + 1) % n == 0) cout << customers[i] << endl; } } int main() { string customers[] = { "Alice", "Bob", "Rayan", "Emma", "Ann", "Bruce", "Synthia", "Daniel", "Richard", "Sam", "Nick", "Mary", "Paul" }; int n; int size = sizeof(customers) / sizeof(customers[0]); // to get the size of the array cout << "Enter the lucky customer number: "; cin >> n; customer_number(customers, size, n); return 0; }
14th Feb 2022, 3:34 AM
Brian H
Brian H - avatar
0
Most of these answers are a bit painful to look at so I included a simple answer that is much easier to read. All you need to do is adjust your forloop to start with 1 instead of 0 and account for the printing statement. void winners(string arr[], int size, int input) { for(int x = 1; x <= size; x++) { if (x % input == 0) cout << arr[x-1] << endl; } }
17th Aug 2022, 12:56 PM
BlueFire1712
BlueFire1712 - avatar
0
#include <iostream> #include <string> using namespace std; void winners(string a[], int b, int n) { for (int x = 1; x<b;x++){ if (x%n == 0){ cout << a[x-1] << "\n"; } } } int main() { string customers[] = {"Alice", "Bob","Rayan", "Emma", "Ann", "Bruce", "Synthia", "Daniel", "Richard", "Sam", "Nick", "Mary", "Paul"}; int n; cin >> n; winners(customers, 13, n); return 0; }
16th Oct 2022, 8:33 PM
Mikita Wagner
Mikita Wagner - avatar
0
#include <iostream> #include <string> using namespace std; //complete the function void winners(string customers[], int size, int n){ int x = n; n--; for(int i=1; i<size; i++){ if (n>size){ break; } cout<<customers[n]<<endl; n += x; } } 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; } To get the code, click on my profile and go down to "See activity". You'll find it in code bits called "lucky test". When you RUN it enter a number and click Submit. Sololearn will not seem to activate for me so I can't message directly, a problem many seem to have. I emailed support and they were not helpful.
4th Nov 2022, 4:53 AM
Scott D
Scott D - avatar
0
i didn't get the output please send correct code
5th Nov 2022, 5:00 AM
Soundarya