Okay, so this is just a small example of something similar I am doing in my own program I am creating for an online class. The only difference is the output of each array will be inputted by user in a different fields instead of having the answer predetermined by the programmer.
In this case after the user numbers are outputted, I want to create a button (which I know how to do, sorry if this is too theoretical right now) which will specific values and output them in a different field.
For example... for aryNumbers[0][0] (Which in my program would be a name) I would call out aryNumbers[0][1] to aryNumbers[0][4] ...so in this case 10, 76, 23.(these numbers would be test scores)
int[ ][ ] aryNumbers = new int[2][4];
aryNumbers[0][0] = 34;
aryNumbers[0][1] = 10;
aryNumbers[0][2] = 76;
aryNumbers[0][3] = 23;
aryNumbers[1][0] = 11;
aryNumbers[1][0] = 30;
aryNumbers[1][0] = 56;
aryNumbers[1][0] = 65;
aryNumbers[2][0] = 34;
aryNumbers[2][0] = 13;
aryNumbers[2][0] = 23;
aryNumbers[2][0] = 18;
int rows = 2;
int columns = 4;
int i, j;
for (i=0; i < rows ; i++) {
for (j=0; j < columns ; j++) {
System.out.print( aryNumbers[ i ][ j ] + " " );
}
System.out.println( "" );
}
Only it would call out all values for the entire list of names and their corresponding test scores.
Aka...
ary..[0][1]
ary..[0][2]
ary..[0][3]
ary..[1][1]
ary..[1][2]
ary..[1][3]
ary..[1][1]
ary..[1][2]
ary..[1][3]
This is my first time working with multi-dimensional arrays..
Change for(j=0... to for(j=1... to skip the zeroth element of the inner array.
Related
I would like to fill an array by looping through an for loop.
Let's say I have:
int8 myArray[30] = {0}; // Declaring and initializing an array which contains maximum 30 elements
Adding elements to an array within a for loop:
for (i = 0; i<5; i++)
{
myArray[0+(i*5)] = getNumberfromFunction1();
myArray[1+(i*5)] = getNumberfromFunction2();
myArray[2+(i*5)] = getNumberfromFunction3();
myArray[3+(i*5)] = getNumberfromFunction4();
myArray[4+(i*5)] = getNumberfromFunction5();
myArray[5+(i*5)] = getNumberfromFunction6();
}
Each Element of the loop shall be filled like:
myArray[0] = getNumberfromFunction1();
myArray[1] = getNumberFromFunction2();
...
...
myArray[5] = getNumberFromFunction6();
myArray[6] = getNumberFromFunction1();
....
....
The first turn with i=0 I, the index is correct:
myArray[0] = ..
myArray[1] = ..
..
..
myArray[5] = ..
The problem starts when i = 1:
Than I will have instead of myArray[6], myArray[5].
The first index in the foor loop will be always overwritten by the last index of the foor loop.
Any suggestions how to handle this?
In each run of your for loop, your are adding six values to the array! Thus, on each subsequent run, your should increment the "offset" count by i*6 (not i*5, as you have done):
for (i = 0; i<5; i++)
{
myArray[0+(i*6)] = getNumberfromFunction1();
myArray[1+(i*6)] = getNumberfromFunction2();
myArray[2+(i*6)] = getNumberfromFunction3();
myArray[3+(i*6)] = getNumberfromFunction4();
myArray[4+(i*6)] = getNumberfromFunction5();
myArray[5+(i*6)] = getNumberfromFunction6();
}
Try this.
You could multiply i by 6 instead of 5.
You are simulating a two-dimensional array on the top of a single dimensional array in C (However, both are laid out contiguously in memory).
You can achieve the same thing by having a two-dimensional array like this:
int array[5][6];
for (row = 0; row < 5; row++) {
for (col = 0; col < 6; col++)
array[row][col] = myFunc();
}
Or if you want to do it in your way, then you need to update your number from 5 to 6 as you are trying to have an array with 5 rows and 6 columns. See this answer to know how multi-dimensional arrays formatted in memory.
for (i = 0; i < 5; i++) {
myArray[0+(i*6)] = getNumberfromFunction1();
myArray[1+(i*6)] = getNumberfromFunction2();
myArray[2+(i*6)] = getNumberfromFunction3();
myArray[3+(i*6)] = getNumberfromFunction4();
myArray[4+(i*6)] = getNumberfromFunction5();
myArray[5+(i*6)] = getNumberfromFunction6();
}
The goal of the loop is to fill each cell over 797 rows across 5 columns A, B, C, D and E with a formula whose cell reference increments by 1.
E.g. Column A rows 6 onwards will have formula "=indirect("'Data Repository'!A3++")"
Column B rows 6 onwards will have formula "=indirect("'Data Repository'!B3++")"
What happens when I run the function however is it only fills in column A. I've checked the execution transcript and execution succeeded is logged after the first column has been filled up. I've tried various variations to no avail.
Below is the last variation I've tested:
function indirect(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Fleet - Weekly V3");
var formulaArray = [];
var columns = ["A","B","C","D","E"];
var row = 2;
var text = '=indirect(\"\'Data Repository\'!';
var headerRow = 6;
var column;
for(i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(i = 0; i < 797; i++) {
row += 1;
if (formulaArray.length == 797) {
sheet.getRange(headerRow, column).offset(0, 0, formulaArray.length).setValues(formulaArray);
} else {
formulaArray.push([formula + row + '")']);
}
Logger.log(formulaArray.length);
}
Logger.log(i)
formulaArray = [];
}
}
Here is where you might be making an error - you need to create the variable i (var i = 0 instead of just i = 0) and if you're nesting loops, you need to have different variables increasing (first loop use i, then nest with j, then nest in that with k etc as needed)
for(var i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(var j = 0; j < 797; j++) {
Untested but I believe it should work if you just substitute that in.
Your problem is in your loops. You are using the 'i' variable twice. Change the for loop that you have nested to iterate over the variable 'j' or something other than 'i'.
I have a buffer with IDs which looks like this
InBuffer={ID1,ID2,ID3,...}
I need to iterate through, every time using the ID in a function that returns pointers to the Data assigned to this ID and the size of the Data. I then need to fill in another buffer with the result which is of the form
OutBuffer={ID1,SIZE1,DATA1.WORD1,...,DATA1.WORDSIZE1,
ID2,SIZE2,DATA2.WORD1,...,DATA2.WORDSIZE2,
...,
IDN,SIZEN,DATAN.WORD1,...,DATAN.WORDSIZEN
}
I am having problems with forming the whole for-loop for this and the indexing of it, mainly because each SIZE variable can be different. It should be simple but I can't seem to make it work.
Thanks in advance for any help.
// For example
// Iterate through the remaining of the Request Buffer (m=0,1 already set)
for (m = 2; m < InBuffer; m++)`
{
OutBuffer[m] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m], &SIZE, &DATA);
OutBuffer[m + 1] = SIZE; // e.g. SIZE = 2, therefore DATA has 2 fields
OutBuffer[m + 2] = DATA.1; // first field
OutBuffer[m + 3] = DATA.2; // second field
// and so on
}
The first thing I notice is that you're using m to index both buffers:
for (m = 2; m < InBuffer; m++)
{
OutBuffer[m]=InBuffer[m];
but then you're using offsets from m for the additional data in OutBuffer:
OutBuffer[m+1]=SIZE;
OutBuffer[m+2]=DATA;
So, what you do think is going to happen in the next iteration of the loop? Say you go through the loop the first time, so that m is 2. The next time, it's m++, i.e. 3, and you make this assignment again:
OutBuffer[m]=InBuffer[m];
But you already assigned something at m[3], and that's the SIZE value from the previous iteration. You also assigned DATA at m[4], and that's going to be overwritten by the SIZE value in this iteration. Eventually, you'll end up with OutBuffer containing exactly what's in InBuffer, plus the SIZE and DATA values for the very last ID.
You need to use a different variable to index OutBuffer, something like:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
OutBuffer[n++] = DATA;
}
There are some other problems as well. For example, the condition in the for loop shouldn't compare m to InBuffer, but should instead compare m and the number of entries in InBuffer. But just straightening out your indexing should be a big step forward.
Update: I just noticed that the data for each ID is larger than just one field. You'll need another loop inside the first one, then, so that you end up with something like this:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
for (i = 0; i < SIZE; i++) {
OutBuffer[n++] = DATA[i];
}
}
If DATA is a structure with fields rather than an array, then you may need a series of if statements to check whether each field should be included or not. You can't use the value of a variable like i as the name of a field, i.e. you can't say DATA.i where i is a variable. I don't think a C structure can have field names that are numbers -- identifiers generally have to start with a letter or underscore, so trying to do that won't make much sense anyway. If you have control over the type of DATA, you should make it an array instead of a structure. So your loop would look more like this:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
i = 0;
if (i++ < SIZE) { OutBuffer[n++] = DATA.field1; }
if (i++ < SIZE) { OutBuffer[n++] = DATA.field2; }
// and so on for each field in DATA's type
}
As Caleb pointed out you should use one variable for each array.
If you're saying that DATA can contain more than one element then you should increase the variable for outBuffer by SIZE each iteration. Also use a loop to asign DATAs fields to OutBuffer
int n = XXX; // set n to the first element you need to assign an ID to
for (m = 2; m < ElementsInBuffer; m++)
{
OutBuffer[n] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n + 1] = SIZE;
for (int i = 0; i < SIZE; i++)
{
OutBuffer[n + 2 + i] = DATA[i]; // works for array only see Calebs answer to see how it works for structs
}
n += SIZE + 1; // +1 to also skip the field for SIZE
}
I'm actually trying to figured out how i can iterate through some objects in a for loop with names like "Object1, Object2, Object3..."
Here is a code to exemplify what i'm trying to do:
for(int i = 0; i <= numberOfObjects; i++) {
someVariable = Object1.value/Object2.value/Object3.value;
}
In this case in the part of the code that i have the Object1,Object2 and Object3 i will change for something, example Object[i].
In this case it can't be done with arrays since i don't know how much objects were created.
For the C++ language
Put the objects into a std::vector.
Your loop will look like:
for (unsigned int i = 0; i < object_vector.size(); ++i)
{
result = result / object_vector[i].value;
}
For the C language
object my_objects[] = {object1, object2, /*...*/};
array_size = sizeof(my_objects) / sizeof(object);
for (unsigned int i = 0; i < array_size; ++i)
{
result = result / my_objects[i].value;
}
Use an array:
std::vector<T> objects{Object1, Object2, Object3};
for (auto&& x : objects) {
someVariable = x.value;
}
There's no other way to do it.
suppose I have a arrays from each i want to produce b these are just examples
a=[4]=> b=[0,4]
a=[3,1]=>b=[0,3,3,4]
a=[2,2]=>b=[0,2,2,4]
a=[2,1,1]=>b=[0,2,2,3,3,4]
a=[3,4,2,5]=>b=[0,3,3,7,7,9,9,14]
I mean when getting 4 it should produce from 0 and then add it to it's content for example 4
or in a[2,1,1] first it will produce 0 and then it see that the next one in a is 1 so after again producing it it will compute 2+1 and assign it.so the output always will be twice size of the input.
i want a pseudo code for it my problem is that when it will repeat I can not write it.
I used JavaScript like syntax.
var a = new Array(3,4,2,5);
var b = new Array();
var bArrayIndex = 0;
b[bArrayIndex] = 0;
bArrayIndex++;
for(i = 0; i < a.length; i++) {
b[bArrayIndex] = b[bArrayIndex-1] + a[i];
if(i < a.length - 1) {
b[bArrayIndex+1] = b[bArrayIndex];
}
bArrayIndex+=2;
}
for(i = 0; i < b.length; i++) {
document.write(b[i] + " ");
}