passing arrays to functions - Who's the lucky winner? | Sololearn: Learn to code for FREE!

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; }

c++

11/13/2020 3:27:22 PM

Radek Skyradek Wlazlo

17 Answers

New Answer

+3

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.

+5

#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; }

+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.

+1

for(int i=0; i<=no;i++) That is the source of error

+1

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; }

+1

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.

0

https://code.sololearn.com/cbT9r55Ul6Av/?ref=app Your code is working 👍 What's the problem?

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 !

0

You hardcoded all possible values of 'n'?

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; }

0

I'm still on this problem and I don't get how this code works

0

#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; }

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

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; }

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

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; }

-1

#include <iostream> #include <string> using namespace std; //complete the function //void winners(string customers[],int no,int 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); if (n<=0 || n>13) { exit(0); } if (n==1) { cout<<customers[0]<<endl<<customers[1]<<endl<<customers[2]<<endl<<customers[3]<<endl<<customers[4]<<endl<<customers[5]<<endl<<customers[6]<<endl<<customers[7]<<endl<<customers[8]<<endl<<customers[9]<<endl<<customers[10]<<endl<<customers[11]<<endl<<customers[12]<<endl; } if (n==2) { cout<<customers[1]<<endl<<customers[3]<<endl<<customers[5]<<endl<<customers[7]<<endl<<customers[9]<<endl<<customers[11]<<endl; } if (n==3) { cout<<customers[2]<<endl<<customers[5]<<endl<<customers[8]<<endl<<customers[11]<<endl; } if (n==4) { cout<<customers[3]<<endl<<customers[7]<<endl<<customers[11]<<endl; } if (n==5) { cout<<customers[4]<<endl<<customers[9]<<endl; } if (n==6) { cout<<customers[5]<<endl<<customers[11]<<endl; } if (n==7) { cout<<customers[6]<<endl; } if (n==8) { cout<<customers[7]<<endl; } if (n==9) { cout<<customers[8]<<endl; } if (n==10) { cout<<customers[9]<<endl; } if (n==11) { cout<<customers[10]<<endl; } if (n==12) { cout<<customers[11]<<endl; } if (n==13) { cout<<customers[12]<<endl; }