how to make array elements shift one to the right?

This is a discussion on how to make array elements shift one to the right? within the C++ Programming forums, part of the General Programming Boards category; // Includes #include<iostream.h> // for << and >> #include<iomanip.h> // for setw and setf #include<fstream.h> // for file I/O #include"apstring.h" ...

  1. #1
    Unregistered
    Guest

    how to make array elements shift one to the right?

    // Includes

    #include<iostream.h> // for << and >>
    #include<iomanip.h> // for setw and setf
    #include<fstream.h> // for file I/O
    #include"apstring.h" // for apstring identifiers
    // Function Prototypes



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~ Main Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~



    int main ()
    {

    cout.setf(ios::fixed, ios::floatfield); // Set up floating point
    cout.setf(ios::showpoint); // output format

    // Declaration and initialization of variables

    float prices[5]; // array of grades
    apstring items[5]; // array of grades
    int quantities[5]; // array of grades
    ifstream old; // Holds the figures
    // Open the files

    old.open("Ald.txt");

    // Get data // Main Function statements

    for (int i = 0; i <= 4; i++)
    {
    old >> items[i];
    old >> prices[i];
    old >> quantities[i];
    }

    cout << " item" << " price" << " quantity" << endl;

    for (i = 0; i <= 4; i++)
    {
    cout << i << " " << setw(6) << items[i] << " " << setprecision(2) << prices[i] << " " << quantities[i] << ' ' << endl;
    }
    cout << endl;


    cout << "Please enter a position value for the price array: ";
    cin >> i;
    cout << prices[i] << endl;

    cout << "Please enter a position value for the quantity array: ";
    cin >> i;
    cout << (quantities[i] -1) << endl;
    int j; // array place
    cout << "Please enter a position value to enter the info to arrays: ";
    cin >> j;
    for ( j; j <= 4; j++)
    {
    items[j]=items[j+1];
    prices[j]=prices[j+1];
    quantities[j]=quantities[j+1];
    }
    cout << "Enter the item (Notebook): ";
    cin >> items[j];
    cout << endl;
    cout << "Enter the price (1.55): ";
    cin >> prices[j];
    cout << endl;
    cout << "Enter the quantity (50): ";
    cin >> quantities[j];
    cout << endl;

    cout << " item" << " price" << " quantity" << endl;

    for (i = 0; i <= 5; i++)
    {
    cout << i << " " << setw(6) << items[i] << " " << prices[i] << " " << quantities[i] << ' ' << endl;
    }
    cout << endl;


    return 0;

    }// end main

    cout << "Please enter a position value to enter the info to arrays: ";
    cin >> j;
    for ( j; j <= 4; j++)
    {
    items[j]=items[j+1];
    prices[j]=prices[j+1];
    quantities[j]=quantities[j+1];
    }
    cout << "Enter the item (Notebook): ";
    cin >> items[j];
    cout << endl;
    cout << "Enter the price (1.55): ";
    cin >> prices[j];
    cout << endl;
    cout << "Enter the quantity (50): ";
    cin >> quantities[j];
    cout << endl;

    i'm an entering new info into say the 3 array element place....how do i make the old 3 information move to 4 and old for to 5??

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    first increase your sizes of the arrays use a const ...

    lets say MAX=6

    get position of array (3)
    from (4) to (3) do
    position (5)=position(4)
    position (4)=position(3)

    get new data.
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    or make it easy and use vector classes:
    Code:
    vector<int> x;
    //fill x with 10 elements
    x.erase(x.begin()+4,x.begin()+4) //remove element 4
    x.insert(x.begin()+4,57); //put # 57 into the space element 4 was

  4. #4
    Unregistered
    Guest

    Red face

    how bout just using the level of ability i'm at.
    i'm in a class and we just started arrays....how could i do this similar to what i have now?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Try something around the lines of this
    Code:
    #include <stdlib.h>
    int move(int array[])
    {
    	int max=sizeof(array)/sizeof(array[1]);
    	int i;
    	for (i=max;i<=0;--i){
    		array[i]=array[i-1];
            }
    	return 0;
    }
    I'm really tired so the above probably has a glaring flaw somewhere.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 10:19 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. make array with command line help
    By Brokn in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 05:10 PM
  4. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 12:26 PM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21