Solved

manipulating array's

Posted on 2003-12-01
7
311 Views
Last Modified: 2010-04-01
i have this so far and want to manipulate them

   const int arraySize = 10;

   int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

e.g. add array value 3 to array value 9
and
e.g. move array value 2 to array value 6

0
Comment
Question by:a_migdal
  • 4
  • 2
7 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 125 total points
ID: 9852211
@a_migdal:

> e.g. add array value 3 to array value 9

     a[2] += a[8];

> e.g. move array value 2 to array value 6

     a[5] = a[1];

What other manipulations did you want?

Hope That Helps,
Dex*
0
 
LVL 1

Expert Comment

by:Gratch06
ID: 9852285
Arrays are a very great way to arrange data for easy manipulation.  Just to clarify a few things that Dexstar wrote, in C/C++, arrays start at 0, not 1.  That means that the first element is a[0], second is a[1], up to a[9] (10 elements total in your example).  

You can do anything to an element of an array that you would do to an integer value.  Addition, multiplication, division, subtraction, or other more complex options are available.  For an example, a[1] is a variable of type int.  Treat it just like you would any integer variable, and you should be able to achieve just about anything you'd like to do.
0
 

Author Comment

by:a_migdal
ID: 9852399
cheers thats a great start for me, what i' aiming to do is something similar to below but c++ confuses me greatly

Instruction                   Description

MOVE Rx,Ry                  move contents of Rx to Ry

MOVE data_value, Rx                                      move data value into  Rx

ADD Rx, Ry                  add Rx to Ry and store it in Ry

SUB Rx, Ry                                  subtract Rx from Ry and store it in Ry



MOVE1 register to register
MOVE2 data to a register
ADD            
SUB

so i can type in add 2 5 and array value 2 will be added to array value 5

cheers for help
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:Gratch06
ID: 9852771
Do you mean that you want at run time to be able to type add 2 5 in and it function correctly?  or at programming time?  While writing code, go to Dexstar's examples.  Those should give you a good idea.  If you want to interpret something typed into your program at execution time, that gets a bit harder.

      cout << "Enter Operation to do, followed by the two arguments." << endl
         << "Example: add 2 5 is the same as array[5] = array[5] + array[2]" << endl;
      cin >> operation >> firstarg >> secondarg;
      firstarg--; secondarg--;  //adjust for array starting at 0.
      if (operation == "mul"){
          a[secondarg] *= a[firstarg];
    } else if (operation == "div"){
          a[secondarg] /= a[firstarg];
    } else if (operation == "add"){
          a[secondarg] += a[firstarg];
    } else if (operation == "sub"){
          a[secondarg] -= a[firstarg];
    } else {
          cout << "Unknown operation." << endl;
    }
    //test function to see output at end.
    for(int i = 0; i < 10; i++){
        cout << a[i] << "  ";
    }
0
 
LVL 1

Expert Comment

by:Gratch06
ID: 9852814
clarification of above...I didn't include variable declarations, but:

string operation;
int firstarg, secondarg;
0
 

Author Comment

by:a_migdal
ID: 9853788
what you have given me is good tar

how can i input multiple instructions to add subtract etc

but exit on the command exit

this is our loop

       while ( operation != 6 ){

       if (operation == 1){
         s[secondarg] += s[firstarg];

             cout << s[secondarg] << "  ";

    } else if (operation == 2){
         s[secondarg] -= s[firstarg];

             cout << s[secondarg] << "  ";

      } else if (operation == 3){
         s[secondarg] = s[firstarg];

             cout << s[secondarg] << "  ";

      } else if (operation == 4){
         s[secondarg] = firstarg;

             cout << s[secondarg] << "  ";

      } else if (operation == 5){
        // s[secondarg] = firstarg;

             cout << s[secondarg] << "  ";
      } /*else if (operation == 6){

            //s[ 9 ] = 101;

      }*/
             else {
         cout << "Unknown operation." << endl;
    }

      //s[ 9 ] += 1;

       }//end of while

thanks
0
 
LVL 1

Expert Comment

by:Gratch06
ID: 9853910
I would put input at the bottom of the loop (and since you'll need initial values, once outside the loop).

iint operation(0);
cout << "Enter Operation to do, followed by the two arguments." << endl
   << "Example: add 2 5 is the same as array[5] = array[5] + array[2]" << endl;
cin >> rawOperation >> firstarg >> secondarg;
operation = getOpCode(rawOperation);
while(operation != 6){
     ...//do usual calculations here
     cout << "Enter Operation to do, followed by the two arguments." << endl
         << "Example: add 2 5 is the same as array[5] = array[5] + array[2]" << endl;
     cin >> rawOperation >> firstarg >> secondarg;
     operation = getOpCode(rawOperation);
}//end of while


the getOpCode function should take the first argument and convert it to a number (since that's what it appears you're trying to do).  I'll leave you to code the body, but here's a prototype for it:

int getOpCode(string inputOperation);

If you'll be using a numerical OpCode instead of the raw string, I'd reccommend just using a switch statement in your main program as it is a little cleaner than a sequence of else if's.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now