Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Pure VPN - Up to 27 Months
vgp
Updated:
Today at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
Write a C++ Program...
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="viraj_slk" data-source="post: 7365396" data-attributes="member: 51932"><p><strong>the logical way to answer this prob.</strong></p><p></p><p><span style="font-size: 12px">hey bro,</span></p><p><span style="font-size: 12px">this is not actually that hard. simply analyze the problem and think logically how to solve it.</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">here i will put one method that i thought of solving this one. there may be more efficient methods as well.</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">First read the value from whatever source, let it be user input or from file and store it in array.:</span></p><p> <span style="font-size: 12px">we will call this array arr[];</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">then count the number of values in the array <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite23" alt="(n)" title="Thumbs down (n)" loading="lazy" data-shortname="(n)" />.:</span></p><p> <span style="font-size: 12px">n = count(arr[])</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">then get the user input for the number of positions to shift the array clockwise (x):</span></p><p><span style="font-size: 12px">x = <em>user input</em></span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">the we use a loop to loop through the current array (arr[]) and build a new array (new_arr[]):</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">[PHP]</span></p><p><span style="font-size: 12px">int p; // the new position for each element in array</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">for(i= 0; i < n; i++){</span></p><p><span style="font-size: 12px"> p = (i + x)%n; // remember n is the number of values in array & x is user input value</span></p><p><span style="font-size: 12px"> </span></p><p><span style="font-size: 12px"> new_arr[p] = arr[i]; </span></p><p><span style="font-size: 12px">}</span></p><p><span style="font-size: 12px">[/PHP]</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">and that's it. your new array with shifted values is ready. now simply loop through the new array output the values to the screen.</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">the key to this problem is the method used to shift the values. which is:</span></p><p><span style="font-size: 12px">[PHP] p = (i + x)%n; // remember n is the number of values in array & x is user input value[/PHP]</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">for example let's say your array is 1,2,3,4,5. and use enters 3 and you have to shift clockwise by 3. </span></p><p><span style="font-size: 12px">so n = 5 (because you have five elements in array)</span></p><p><span style="font-size: 12px">x = 3 (because user entered 3)</span></p><p><span style="font-size: 12px"></span></p><p><span style="font-size: 12px">and if we consider number 2 in the array, it's original position within the array is 1. remember arrays start with 0:</span></p><p><span style="font-size: 12px">so arr[1] => 2</span></p><p><span style="font-size: 12px">so in the above loop when i equals to 1, arr<em> equals to 2</em></span></p><p><span style="font-size: 12px"><em>so (i + x) => 1 + 3 => 4</em></span></p><p><span style="font-size: 12px"><em>and (i + x)%n => 4%5 => 4</em></span></p><p><span style="font-size: 12px"><em>so the new positions (p) of the value 2 is => 4</em></span></p><p><span style="font-size: 12px"><em>therefore new_arr[p] => new_arr[4] => 2</em></span></p><p><span style="font-size: 12px"><em></em></span></p><p><span style="font-size: 12px"><em>The problem is theoretically solved. Now you should be able to write the code yourself. It's better when you do that by yourself, otherwise you will never get the hang of these things. I know its hard at first. But you get it once you do more and more stuff like these. Trust me! <img src="/styles/default/xenforo/smilies/default/happy.gif" class="smilie" loading="lazy" alt=":)" title="Happy :)" data-shortname=":)" /> </em></span></p><p><span style="font-size: 12px"><em></em></span></p><p><span style="font-size: 12px"><em>Let me know if you have any trouble writing the code.</em></span></p></blockquote><p></p>
[QUOTE="viraj_slk, post: 7365396, member: 51932"] [b]the logical way to answer this prob.[/b] [SIZE="3"]hey bro, this is not actually that hard. simply analyze the problem and think logically how to solve it. here i will put one method that i thought of solving this one. there may be more efficient methods as well. First read the value from whatever source, let it be user input or from file and store it in array.: we will call this array arr[]; then count the number of values in the array (n).: n = count(arr[]) then get the user input for the number of positions to shift the array clockwise (x): x = [I]user input[/I] the we use a loop to loop through the current array (arr[]) and build a new array (new_arr[]): [PHP] int p; // the new position for each element in array for(i= 0; i < n; i++){ p = (i + x)%n; // remember n is the number of values in array & x is user input value new_arr[p] = arr[i]; } [/PHP] and that's it. your new array with shifted values is ready. now simply loop through the new array output the values to the screen. the key to this problem is the method used to shift the values. which is: [PHP] p = (i + x)%n; // remember n is the number of values in array & x is user input value[/PHP] for example let's say your array is 1,2,3,4,5. and use enters 3 and you have to shift clockwise by 3. so n = 5 (because you have five elements in array) x = 3 (because user entered 3) and if we consider number 2 in the array, it's original position within the array is 1. remember arrays start with 0: so arr[1] => 2 so in the above loop when i equals to 1, arr[i] equals to 2 so (i + x) => 1 + 3 => 4 and (i + x)%n => 4%5 => 4 so the new positions (p) of the value 2 is => 4 therefore new_arr[p] => new_arr[4] => 2 The problem is theoretically solved. Now you should be able to write the code yourself. It's better when you do that by yourself, otherwise you will never get the hang of these things. I know its hard at first. But you get it once you do more and more stuff like these. Trust me! :) Let me know if you have any trouble writing the code.[/i][/SIZE][i][/i] [/QUOTE]
Insert quotes…
Verification
Hathara warak wissa keeyada? (Hathara wadi karanna 20)
Post reply
Top
Bottom