moving values within an array

i need to be able to move the last significan digit of an array to the

Page 1 of 1

5 Replies - 1183 Views - Last Post: 31 January 2008 - 04:59 AM Rate Topic: -----

#1 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

moving values within an array

Posted 30 January 2008 - 04:58 PM

I am having problems trying to move the contents of an array to the rear of the array. I havent written all of main yet but it will not affect my problem.


#include <iostream>
#include <cctype>
using namespace std;

//function prototypes
void inputNumber(int _num[], int &_size); //allows user to input # and deter size.
void initialize(int number[], const int size); // initializes any array to 0
void shift(int number[], const int size); //moves number to the end of the array
bool add(const int num1[],const int num2[], int sum[], const int size);//adds the numbers
void display(const int number[], const int size);//displays the arrays

//global constants
const int MAX = 20;

int main()
{
	int num1[MAX];
	int num2[MAX];
	int sum[MAX];
	int size1, size2;//tells what position the last significant digit is

	initialize(num1, MAX);
	inputNumber(num1, size1);
	shift(num1, size1);
	
	display(num1, MAX);
	
	
	return 0;
}


//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
void inputNumber(int _num[], int &_size)
{
 
	char ch, quit='n', flag='f';
		_size=0;
		do {
			if(_size>=MAX)
				flag='t';
			cin.get(ch);
			if(isdigit(ch))
				 _num[_size++]= ch - 48;
			else
				quit='y';
		}while(quit!='y');
		if(flag=='t') 
			_size =-1;
}



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//I dont see the need for this function if I were to initialize the arrays in main

void initialize(int number[], const int size)
{
	for (int x=0; x<MAX; x++)
		number[x]= 0;



}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void display(const int number[], const int size)
//dont see why I need to have the const int size
{
	for (int x=0; x<MAX; x++)
		cout<<number[x];
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void shift(int number[], const int size)
{
	int temp, temp2;
	for (int x=0;x<MAX;x++)
	{
		temp=number[x];
		temp2=number[MAX];
		number[MAX-size]=temp;
		
	}
}



for example if i enter into the cin portion 12345 the rest gets filled in with zerosso it looks like
12345000000000000000 i need to revers it so it looks like 00000000000000012345. That way I can repeat my code to enter and add my arrays together wit the unwritten add function.

Is This A Good Question/Topic? 0
  • +

Replies To: moving values within an array

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: moving values within an array

Posted 30 January 2008 - 11:48 PM

I am concentrating on this function :

void shift(int number[], const int size)
{
	int temp, temp2;
	for (int x=0;x<MAX;x++)
	{
		temp=number[x];
		temp2=number[MAX];
		number[MAX-size]=temp;
		
	}
}



you need to modify this function, because what you are doing here is just replacing the last 'size' numbers with first 'size' numbers. this is not shifting of numbers. and this will fail in case when you have entered more than 50% of max amount in array.

say array MAX is of 10 and you entered 8 numbers, then calling shift function will place your 0th number to MAX-size = 2nd place 1st to 3rd place, but now you have replaced 2nd number with 0th!!! so you lost it and the 2nd number which is actually the 0th number will be again placed at 4th place!!! and this will repeat till the loop ends :blink: . got my point?

Now in this case there are 2 solutions. one is faster but requires more memory. and the other is bit slower than first but requires just one more int. I will go with the second first. :)

void shift(int number[], const int size)
{
	int temp;
   for (int y=0;y<size;y++)
   {
	temp=number[y];
	for (int x=0;x<MAX-1;x++)
	{
	  number[x] = number[x+1];		
	}
	number[MAX-1]=temp;
  }
}



in the other way we detach these nested loops to improve on speed but it takes bit more memory.

void shift(int number[], const int size)
{
	int temp[size];
   for (int y=0;y<size;y++)
   {
	temp[y]=number[y];
   }

	for (int x=0, int y=size;x<MAX-size;x++,y++)
	{
	  number[x] = number[y];		
	}
	for (int x=MAX-size, int y=0;x<MAX;x++,y++)
	{
	  number[x] = temp[y];		
	}	
  }
}



I hope this will help you. ;)
Was This Post Helpful? 0
  • +
  • -

#3 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: moving values within an array

Posted 31 January 2008 - 12:00 AM

-deleted- already answered by AmitTheInfinity

This post has been edited by Nayana: 31 January 2008 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: moving values within an array

Posted 31 January 2008 - 04:11 AM

AmitTheInfinity', THank you for you help however it does not solve my problem. the code you wrote the first one only moves every other number say i typed in 12345 it produced 00000000000000013500 which isnt what I really wanted. The second code does not compile.... over 9 errors.but it was a start
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: moving values within an array

Posted 31 January 2008 - 04:47 AM

View Postrudy5453, on 31 Jan, 2008 - 04:41 PM, said:

AmitTheInfinity', THank you for you help however it does not solve my problem. the code you wrote the first one only moves every other number say i typed in 12345 it produced 00000000000000013500 which isnt what I really wanted. The second code does not compile.... over 9 errors.but it was a start



Hey, I made a silly mistake there... :crazy:

have this code now and try whether it works, actually I am writing the code directly in the message window here and I am not compiling or trying to run them due to lack of time. sorry for that.

void shift(int number[], const int size)
{
	int temp;
   for (int y=0;y<size;y++)
   {
	temp=number[0];		// You should ALWAYS pick the first number.  I am such an idiot there...
	for (int x=0;x<MAX-1;x++)
	{
	  number[x] = number[x+1];		
	}
	number[MAX-1]=temp;
  }
}




I don't know what errors you got for the second there but I will try to make it as secure as possible here...

void shift(int number[], const int size)
{
	int temp[MAX],x,y;
   for (y=0;y<size;y++)
   {
	temp[y]=number[y];
   }

	for (x=0, y=size;x<MAX-size;x++,y++)
	{
	  number[x] = number[y];		
	}
	for (x=MAX-size, y=0;x<MAX;x++,y++)
	{
	  number[x] = temp[y];		
	}	
  }
}




OK, now I hope that this time it will work fine. tell me whether it helped you...

This post has been edited by AmitTheInfinity: 31 January 2008 - 04:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: moving values within an array

Posted 31 January 2008 - 04:59 AM

Hey, I made a silly mistake there... :crazy:

have this code now and try whether it works, actually I am writing the code directly in the message window here and I am not compiling or trying to run them due to lack of time. sorry for that.

void shift(int number[], const int size)
{
	int temp;
   for (int y=0;y<size;y++)
   {
	temp=number[0];		// You should ALWAYS pick the first number.  I am such an idiot there...
	for (int x=0;x<MAX-1;x++)
	{
	  number[x] = number[x+1];		
	}
	number[MAX-1]=temp;
  }
}




OK, now I hope that this time it will work fine. tell me whether it helped you...
[/quote]

Thank you for everything this code works perfectly. It is also easy to read and follows a logical path that I can understand. You have been a great help in teaching me this. Many Thanks. :D B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1