I created a menu that one of the option is "calculate average number"
I have a 2000 rows of numbers in a text file.
I use fstream and put them in array but I had to set the array size as 2100 because there is another option which is
" Add a new employee " which means if I add a new employee detail it will become 2001 rows.
So the problem is how should I check the number of rows which filled with numbers?
I tried search for some solution and some are these
int rows = sizeof(arr)/sizeof(arr[0]);
but instead of show the current number of rows which filled with numbers it shows the array size I set (2100)
You can use a std::vector. Add elements to it using push_back(), get the number of elements using size().
If you want to still use arrays, the logic that reads data would need to return the number of entries it read (e.g. size_t n = readLines("myfile.txt"); ). Or if you know of some value you'll never encounter you can prefill the array with that value (e.g. -1) so when processing the read data you can stop once that value is found. However, be aware the array method might someday fail if you don't have enough space for all the entries in the file.
Related
I am using Office 365 currently and I want to make a visualization tools using MAKEARRAY functions.
For example, if I want to display sequential of 32 items, I would display it in this way:
I use the following formula of Makearray to generate the custom array for me
Note: Formula is pasted at cell value F3 .
=MAKEARRAY(ROUNDUP(B2/B3,0),IF(E3#=ROUNDUP(B2/B3,0),MOD(B2,B3),B3),LAMBDA(row,col,"O"))
but it seems like after debugging, this part of the formula are giving it the problem are these
IF(E3#=ROUNDUP(B2/B3,0),MOD(B2,B3),B3)
as I debugging the formula separately as shown in picture below, it can generate the correct amount of columns as it is supposed to.
Note: Generate exactly same amount to the no of columns if row number is not matching;
Generate modulus remainder formula if row number is matching to roundup of no. of items divided by no. of columns.
But in the end, I put that problematic formula back into the MAKEARRAY function just give only a single columns, which seems like it is quite wrong.
May I know why it display single columns even though by right, it should display the correct amount of no. of columns?
What about:
Formula in C1:
=WRAPROWS(INDEX("O",SEQUENCE(A1,,,0)),A2,"")
Or rather:
=WRAPROWS(EXPAND("O",A1,,"O"),A2,"")
MAKEARRAY does not expect an array in the number of columns. It is a set number. It will iterate the number of rows and number of columns to create the array. It will always be square and not jagged.
So you need to do the math to change the value:
=MAKEARRAY(ROUNDUP(B2/B3,0),B3,LAMBDA(rw,clm,IF(10*(rw-1)+clm>B2,"","O")))
Now as soon as the space is greater than the 32 it puts in "" instead of "O"
In the passing of a 2D array into a function, why do I have to specify the column size (i.e. how many columns does my array have)?
Can it work the other way round- instead of specifying the column size I specify the number of rows?
Consider a table into which you haven't entered any data.
If you do not specify the number of columns, every time you enter new data, it goes into the next column in the first row, and this goes on until all the data is entered. But then all the data is entered in the first row itself, making your table a single row, that's a 1-D array, not a 2-D array.
But if you specify the number of columns, when you hit the limit when entering data, the next piece of data goes to the next set of columns (i.e. the 2nd row) automatically, thus a table is formed.
No, you cannot specify only the number of rows.
Hope I helped!
I have a function that returns a matrix of information that I want to store in a cell, however no matter how I try this it keeps giving me various errors or the results are incorrect, my latest attempt is shown below:
bbag=[]
for j=3:100
bag=rand(randi([1 5]),randi([1 5]))%stand in for more complex function that normally returns between 1 and 4
[dontcare,y] = size(bag);
tbag={bag(1,1),bag(2,1),bag(3,1),bag(4,1),bag(4,1)}
for i=2:y
tbag=[tbag,{bag(1,i),bag(2,i),bag(3,i),bag(4,i)}]; %some kind of loop is probably required here
end
bbag=vertcat(bbag,tbag)
labels(i) = 1;
end
but this cannot handle when the data contains anything other than 4 data columns and if it does it only manages to append all the data on the same row not put it in it's own cell, any idea how to do this such that by the end I can ask for bbag(2,3) and then return a cell containing between 1 and 5 values? If I fix the sizes to be 4 then I instead get a 98-by-17 cell block (rather then 98x4x4 that I would expect). Any ideas?
In
bag=rand(randi([1 5]),randi([1 5]));
you create a random matrix with random size (i.e. random number of rows and columns). I don't know if the errors have anything to do with cell array creation; they have everything to do with the fact that you access e.g. row 3 from bag without making sure that you actually have 3 rows in it.
Also, check the doc for mat2cell for splitting a matrix into a cell array of heterogeneous-size matrices (if this is indeed what you're looking for).
I've only just started using arrays (as in yesterday) and have tried ambitiously to use them in a work spreadsheet but am having issues. Details are as follows:
I have a table of data with column R containing a list of room numbers (where each room number can appear more than once and each instance needs to be counted.). I want to then count how many of those room numbers are also in my list in column Z(particular room type).
I can do this by checking each room number individually but was wondering if it was possible to do with this by checking the entire array at once.
I mocked up an array formula to accomplish the task. Probably not the best implementation but it was quick. As always you need to enter an array formula with CTRL + SHIFT + ENTER. Note that I assumed each column had 15 rows and started in row 1.
Count Duplicates Independently
=SUM(1*NOT(ISERROR(MATCH(R1:R15,$Z$1:$Z$15,0))))
Count Duplicates As One
=SUM(IFERROR(1/COUNTIF(R1:R15,R1:R15),0)*NOT(ISERROR(MATCH(R1:R15,$Z$1:$Z$15,0))))
The formula breaks down like so...
IFERROR(1/COUNTIF(R1:R15,R1:R15),0)
Returns 1/(# of occurrences of a given cell value in all of column R). This allows us to sum up 1/3 + 1/3 + 1/3 if a room number is in column R three times (so we only count it once).
NOT(ISERROR(MATCH(R1:R15,$Z$1:$Z$15,0)))
Returns TRUE if the given column R cell value occurs in column Z and FALSE otherwise. When we multiply this time the first part, TRUE becomes 1 and FALSE becomes 0.
=SUM(...)
Sum up all the intermediate products.
Courtesy of #Scott Craner who suggested:
=Sumproduct(COUNTIF(R2:R1709,Z2:Z199))
Thanks also to #Mark Balhoff as well.
I need your help or a little advice with my problem. For example, have a table looking like this:
blahbla 4 5 7 44
lololol 8 7 8 45
kokooko 1 2 3 4
These table has 3 lines and 4 columns, but the number of lines and columns may vary. I need to read values from this table (it is no problem with fopen) but the problem is that i dont know how to access concrete values from this table. For example if I want to printf values only from first line, or only from third column, what am I supposed to do? give me please some advice without using malloc, thanks.
Find matrix size
At first you should find the matrix size. Rows will be easy, you only have to count number of lines (I'm assuming that you are reading data from .txt file). Next thing is columns. If all rows will have the same number of elements, you can iterate over the string that is the first line and check if elements of the string are letters (for example with isalpha function). If element is a letter, then you can increment the number of columns - if not, then it will be space or number, if it is a space then you should increment number of columns variable, and if it is not then you have to skip to the next element.
If the number of elements in rows of your table may vary, then you should iterate over every line and find the number of columns like in previous case, finding the maximum.
Create matrix and copy data
Now, when you have size of your matrix you can allocate it like this: string table[numberOfRows][numberOfColumns] - then you should once again iterate over the data in txt file, checking if the element in line is alpha or is space. If it is a space and the next character is numeric(you can check it with isdigit and isspace), then you should make some string variable that you will be concatenating until it hits another space or end of line - then you assign it to matrix. If it is letter then it will be easy, just assign it to proper place in the matrix. Remeber that you will have to fill additional elements in the matrix with something like "0" (of course only if the rows can have different number of elements).
Retrieve data
You can retrieve data only from column or a row, simply create an array that has size of your table columns or rows, then you can for example iterate over your table and check if the row(column) is the one that is interesting you. And if it is, assign it to your array. Then you can print it with another loop.