473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Flipping a section of an array

20
Hey, another question. How do you flip a section of a one-dimensional array? For example, you have array[5] = {1,2,3,4,5}, and I want to flip the last 3 numbers, to produce array[5] = {1,2,5,4,3}.

Any help would be much appreciated!
Jan 30 '07 #1
5 1959
r035198x
13,262 8TB
Hey, another question. How do you flip a section of a one-dimensional array? For example, you have array[5] = {1,2,3,4,5}, and I want to flip the last 3 numbers, to produce array[5] = {1,2,5,4,3}.

Any help would be much appreciated!
Just write a function that does it. The function should take the array, the size of the array, the start position and the end position for the swap. Let's see what you think it should be like first.
Jan 30 '07 #2
Acolyte
20
Alright, here's what I've got so far for it:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main (void)
  3.  
  4. {
  5.         int array[5], temparray[5], f, l, flip;
  6.         printf("Please enter the numbers: ");
  7.         for (f = 0; f < 5; f ++)
  8.  
  9.         {
  10.                 scanf("%d", &array[f]);
  11.                 temparray[f] = array[f];
  12.         }
  13.  
  14.         printf("Please enter the number to start the flip from: ");
  15.         scanf("%d", &flip);
  16.  
  17.         l = 5;
  18.  
  19.         for (f = flip; f < 6; f ++)
  20.         {
  21.                 array[f] = temparray[l];
  22.                 array[l] = temparray[f];
  23.                 l--;
  24.         }
  25.  
  26.         for (f = 0; f < 5; f ++)
  27.         {
  28.                 printf("%d ", array[f]);
  29.         }
  30.  
  31. }
  32.  
Jan 30 '07 #3
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. for (f = flip; f < 6; f ++)
  2. {
  3.    array[f] = temparray[l];
  4.    array[l] = temparray[f];
  5.    l--;
  6. }
Two problems here:

1) Your loop goes from flip to 5. This will result in accessing the 6th member of each array (which hasn't been defined). You should instead write for (f = flip; f < 5; f++) so that you will never try to access array[5] (6th element).

2) This loop, as it is, will flip the numbers - twice. Your array will end up the same as before. This is because you are visiting each element to be flipped and flipping it - but in each execution, two numbers are being flipped, not just one.
Jan 31 '07 #4
Manjiri
40
Alright, here's what I've got so far for it:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main (void)
  3.  
  4. {
  5.         int array[5], temparray[5], f, l, flip;
  6.         printf("Please enter the numbers: ");
  7.         for (f = 0; f < 5; f ++)
  8.  
  9.         {
  10.                 scanf("%d", &array[f]);
  11.                 temparray[f] = array[f];
  12.         }
  13.  
  14.         printf("Please enter the number to start the flip from: ");
  15.         scanf("%d", &flip);
  16.  
  17.         l = 5;
  18.  
  19.         for (f = flip; f < 6; f ++)
  20.         {
  21.                 array[f] = temparray[l];
  22.                 array[l] = temparray[f];
  23.                 l--;
  24.         }
  25.  
  26.         for (f = 0; f < 5; f ++)
  27.         {
  28.                 printf("%d ", array[f]);
  29.         }
  30.  
  31. }
  32.  

Hello Friend... This logic will help you to get the result...
So try it out... I have done little modification in your code.. And whole logic resides in while loop....

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int  main (void)
  3.  
  4. {
  5.         int array[5],i,flip,n,f,l,temp;
  6.         printf("How many numbers\n");
  7.         scanf("%d",&n);
  8.         printf("Please enter the numbers: ");
  9.         for(i=0; i<n; i++)
  10.         scanf("%d",&array[i]);
  11.         printf("Please enter the number to start the flip from: ");
  12.         scanf("%d", &flip);
  13.  
  14.  
  15.         f=flip;
  16.         l=n-1;
  17.         while(f<l)
  18.      {
  19.  
  20.         temp=array[f];
  21.         array[f]=array[l];
  22.         array[l]=temp;
  23.         printf("%d\n%d\n",array[f],array[l]);
  24.         f++;
  25.         l--;
  26.       }
  27.         printf("After flipping the array is\n");
  28.         for(i=0; i<n; i++)
  29.         printf("%d",array[i]);
  30.  
  31. return 0;
Jan 31 '07 #5
Acolyte
20
Alright, I've made the modifications, and got parts of the array to flip. Now, the next part.
By the way, I'm supposed to be working towards creating a Pancake Sorting program, so if anybody has any help at all for that, I'd love to see it.

Anyways, here's my next hurdle-I'm trying to get it to find the largest number, have it switch places with the first number, then invert the whole array. As usual, here's what I have:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main (void)
  3.  
  4. {
  5.         int array[5], temparray[5], f, g, l, flip, lgp, lgn = 0;
  6.         printf("Please enter the numbers: ");
  7.         for (f = 0; f < 5; f ++)
  8.  
  9.         {
  10.                 scanf("%d", &array[f]);
  11.                 temparray[f] = array[f];
  12.         }
  13.  
  14.         l = 4;
  15.  
  16.         for (f = 0; f < 6; f ++)
  17.         {
  18.                 if (array[f] > array[f + 1])
  19.                         lgp = f;
  20.                         lgn = array[f];
  21.         }
  22.  
  23.         array[0] = lgn;
  24.         array[lgp] = temparray[0];
  25.  
  26.  
  27.         for (f = 0; f < lgn; f ++)
  28.         {
  29.                 array[f] = temparray[l];
  30.                 l--;
  31.         }
  32.  
  33.         for (f = 0; f < 5; f ++)
  34.         {
  35.                 printf("%d ", array[f]);
  36.         }
  37.  
  38. }
Jan 31 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Peter Oliphant | last post by:
> The flicker is because you background is painted first to clear the > background and then your graphics shapes. > Flicker could be avoided by using a memory device context. where you draw > the...
3
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
6
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main...
5
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hello, I have what I thought was a simple problem, and perhaps there is still a simple solution. I have a page that is using local resource files to store all the necessary control properties on...
7
by: arnuld | last post by:
this is the programme which converts a string of digits into its numeric equivalent, given in section 2.7: /* atoi: convert s to integer */ int atoi(char s) { int i, n; n = 0; for (i = 0;...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
2
by: sck10 | last post by:
Hello, How do you reference an array in the App_Code section? I tried using SystemColors, but got the error: 'System.Drawing.SystemColors' is a 'type' but is used like a 'variable'. Thanks,...
0
by: towerjj | last post by:
hi all, I need a configuration file mapped to a class in my web app. It is an array of a class, in XML something like this: <ArrayOfRole> <Role> <name>r1</name> <sections>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.