Question about functions in C ++ - Programming - HWzone forums
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Question about functions in C ++


Recommended Posts

Hi friend

I have a question about the functions I need to take an array between N cells (N = 6) and check if there is a value in the array that divides all the cells to its left and all the cells from it are divided by it

If there is such a cell the output should be the first cell number (when there are several cells then the first cell) if not then the output will be -1.

 

This is the code I wrote and for some reason the output is blank..I would love to help what the problem is with this code

 

// ------ include section ------- #include #include // ------ std section ------- using std :: cin; using std :: cout; using std :: endl; // ------ const section const int N = 6; // The size you want to set // --- prtotypes section ---- void read_data (int arr []); int divide_left (int arr []); int divide_right (int arr []); void happend (int arr []); ///// ----- Main ------- int main () {int arr [N]; read_data (arr); happend (arr); return EXIT_SUCCESS; } void read_data (int arr []) {for (int i = 0; i <N; i ++) cin >> arr [i]; } int divide_left (int arr []) {int divide_l = arr [0]; for (int i = 1; i <N; i--) {if ((arr [i]% arr [i-1]) == 0) {divide_l = i; }} return divide_l; } int divide_right (int arr []) {int divide_r = arr [0]; for (int i = 1; i <N; i ++) {if ((arr [i + 1]% arr [i]) == 0) {divide_r = i; }} return divide_r; } void happend (int arr []) {int divide_l = divide_left (arr); int divide_r = divide_right (arr); if (divide_l == divide_r) cout << divide_l; else cout << "-1"; }

 

Thanks again to everyone! :)

Post Reply Direct Link To This Post
Share on other sites
  • תגובות 42
  • Opens on
  • Last comment

Prominent participants in the discussion

Prominent participants in the discussion

Popular responses

It's on a standard that even a broken watch is accurate twice a day. I see that already yesterday they wrote to you exactly why it does not work, I did not see that you did anything to fix the matter not even in the last code  

Now your code does not call divide_left at all as for the rest of the code adding loops but in my opinion they are not in the right place, it is impossible to look for a division solution from one direction and then look for a division solution from the other direction in two separate searches

I fixed the code. It's OK? // ------ include section ------- #include #include // ------ std section ------- using std :: cin; using std :: cout; using std :: endl; // -----

I do not know if there are any more problems, but this is definitely an error:

for (int i = 1; i <N; i--)

Either go from beginning to end with I ++ or from end to beginning with I--. It currently starts from 1 and drops to negative numbers creating an almost infinite loop.

Post Reply Direct Link To This Post
Share on other sites
Quote of QttP

I do not know if there are any more problems, but this is definitely an error:



for (int i = 1; i <N; i--)

Either go from beginning to end with I ++ or from end to beginning with I--. It currently starts from 1 and drops to negative numbers creating an almost infinite loop.

 

First of all thank you very much I changed it to start from N -1 and then go down But still it does not print anything, I would love for a little more guidance to those who can.

 

Thanks :)

Edited By BAR_PC
Post Reply Direct Link To This Post
Share on other sites
Quote from BAR_PC

 

First of all thank you very much I changed it to start from N -1 and then go down But still it does not print anything, I would love for a little more guidance to those who can.

 

Thanks :)

I did not go over the rest of the code, but did you also change the "i smaller than N" to "i larger equal to 0"?

Post Reply Direct Link To This Post
Share on other sites
Quote by af db creid

What input do you enter?

Also, does the program stop and print nothing or get stuck?

Input 15 20 30 60 120 180 and the program prints -1 instead of the cell value

This is the code:

// ------ include section ------- #include #include // ------ std section ------- using std :: cin; using std :: cout; using std :: endl; // ------ const section const int N = 6; // The size you want to set // --- prtotypes section ---- void read_data (int arr []); int divide_left (int arr []); int divide_right (int arr []); void happend (int arr []); ///// ----- Main ------- int main () {int arr [N]; read_data (arr); happend (arr); return EXIT_SUCCESS; } void read_data (int arr []) {for (int i = 0; i <N; i ++) cin >> arr [i]; } int divide_left (int arr []) {int divide_l = arr [N]; for (int i = N -1; i> = 0; i--) {if ((arr [i]% arr [i-1]) == 0) {divide_l = i; return divide_l; }} // return divide_l; } int divide_right (int arr []) {int divide_r = arr [0]; for (int i = 1; i <N; i ++) {if ((arr [i + 1]% arr [i]) == 0) {divide_r = i; return divide_r; }} // return divide_r; } void happend (int arr []) {int divide_l = divide_left (arr); int divide_r = divide_right (arr); if (divide_l == divide_r) cout << divide_l; else cout << "-1"; }

 

Edited By BAR_PC
Post Reply Direct Link To This Post
Share on other sites

just a minute -

Quote from BAR_PC

That it divides all the cells on its left and all the cells in its hydrogen divide in it

That is, all the cells in the array are divided by it?

Because "he divides X" and "X divides it" are pretty much the same.

Edited By af db creid
Post Reply Direct Link To This Post
Share on other sites

Join the discussion

You can then join the discussion and then join our community. If you already have an account with us, please Log in now To comment under your username.
Note that: The comment will appear to the surfers after approval by the board management team.

guest
Add a comment

×   The content pasted is with formatting.   Remove formatting

  Only 75 emoji are allowed.

×   Your link has been automatically assimilated.   Show as regular link

×   Your previous content has been automatically restored.   Clear all

×   You can not paste images directly. Upload or insert images from URL.


×
  • Create new ...

At the top of the news:

new on the site