moving data within an array C++

put data squentially at the end of an array

Page 1 of 1

5 Replies - 8174 Views - Last Post: 31 January 2008 - 04:43 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 data within an array C++

Post icon  Posted 30 January 2008 - 07:36 PM

i havent finished writing the functions main and for the bool add. those shouldnt give me too much of a problem after i figure out how to manipulate the array. I am stuck...
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

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


}



Is This A Good Question/Topic? 0
  • +

Replies To: moving data within an array C++

#2 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: moving data within an array C++

Posted 30 January 2008 - 08:23 PM

You could create another array, as long as the one from which you're copying data, and iterate through the source array until you reach a non zero number and then add that to the start of the new array.
int copy[MAX]
int iter = 0;
for (int i = 0; i < MAX; i++)
{
	if (num1[i] != 0)
		copy[iter++] = num1[i];
}


This post has been edited by GWatt: 30 January 2008 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 rudy5453  Icon User is offline

  • New D.I.C Head

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

Re: moving data within an array C++

Posted 30 January 2008 - 09:54 PM

View PostGWatt, on 30 Jan, 2008 - 08:23 PM, said:

You could create another array, as long as the one from which you're copying data, and iterate through the source array until you reach a non zero number and then add that to the start of the new array.
int copy[MAX]
int iter = 0;
for (int i = 0; i < MAX; i++)
{
	if (num1[i] != 0)
		copy[iter++] = num1[i];
}




the only problem with that is what if someone enters 12340 i still need that last 0...
Was This Post Helpful? 0
  • +
  • -

#4 WXY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 02-January 08

Re: moving data within an array C++

Posted 30 January 2008 - 10:29 PM

Simple.
1) Since _size is already recorded as how many valid inputs the user has made, either make it a global variable or make "inputNumber' return it

2) this function will take your array and a user input size and shift it (within itself, so you don't need a 2nd buffer, unless you really want one)
void ComminceShift(int *bufferIn, int size) {
memmove(&(bufferIn[MAX-size]), bufferIn, size*sizeof(int));
memset(bufferIn, 0, (MAX-size)*sizeof(int));
}



PS: change MAX to a #define. It optimizes better for some compilers. Sorry if I screwed up with some numbers, i've been using my brain too much today

This post has been edited by WXY: 30 January 2008 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 rudy5453  Icon User is offline

  • New D.I.C Head

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

Re: moving data within an array C++

Posted 31 January 2008 - 04:15 AM

wow WXY that code is well beyond me I cant even understand what is going on there... Thanks for the help though
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: moving data within an array C++

Posted 31 January 2008 - 04:43 AM

View PostWXY, on 31 Jan, 2008 - 05:29 AM, said:

PS: change MAX to a #define. It optimizes better for some compilers.

That's a bad idea - The first rule of optimisation is "Don't".

using #define for constants is bad style in C++, since #define is not type checked, so const variables should always be preferred for creating global constants. Optimisation is usually irrelevant as an argument of why you should do something unless you know from profiling, that something is causing a loss in performance or eating up resources in an unacceptable way in your program. Usually, you can't tell until the later stages of development

The #define guideline may be different for C, since some legacy compilers (based on K&R C) do not support the const keyword.

This post has been edited by Bench: 31 January 2008 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1