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.
Related
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.
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 wanted to know how to get row and column totals from a 2D array in Excel. This is a fairly common thing to do but I couldn't find an answer to it by searching on row and column totals so I thought it would be worth posting it as a question.
Supposing I wanted to find the lowest column total and highest row total in the following array which is in cells A1:D3:-
1 2 3 4
5 6 7 8
9 10 11 12
my initial thoughts were along the lines of
=min(A1:D3*(column(A1:D3)={1,2,3,4}))
but this kind of simple approach doesn't work. I remembered reading that you had to use mmult in some of these situations and have seen advanced formulae using them but couldn't quite remember how. I shall try and answer my own question but other suggestions are more than welcome.
You can do it with MMULT as you mentioned. The following should work with your setup:
Smallest column
=MIN(MMULT({1,1,1},A1:D3))
Largest row:
=MAX(MMULT(A1:D3,{1;1;1;1}))
Note how many 1s in the array - for the rows calc you need a 1 for each column (i.e. 3) and vica versa for columns. Also note the order of the arrays - it won't work the other way around
Yes you have to mmult to deliver either a column array or row array containing the required totals, then use can use MAX, MIN or any other aggregate function to get the value you require.
Column totals
=MIN(MMULT(TRANSPOSE(ROW(A1:D3))^0,A1:D3))
Row Totals
=MAX(MMULT(A1:D3,TRANSPOSE(COLUMN(A1:D3))^0))
So the idea is that you create a single-row array {1,1,1} and multiply it by the 2D array to end up with an array {15,18,21,24} and take the minimum value from it.
Or create a single-column array {1;1;1;1} and multiply the original array by it to end up with an array {10;26;42} from which you get the maximum value.
Remember that mmult works like the matrix multiplication you might have learned at college where for each cell it works across the cells in the corresponding row of the first array and down the cells of the corresponding column in the second array multiplying each pair and adding them to the total. So the number of columns in the first array must always equal the number of rows in the second array.
These are, as #Scott Craner reminds me, array formulae that have to be entered with
Ctrl Shift Enter
I'm having problems reading in an unknown size array from a txt file in C. The txt file contains values separated by spaces, and rows start every new line. For example:
1 2 3
4 5 6
7 8 9
My problem is that I can "hardcode" something like for i, j =100000 and read in a matrix assuming 100000 is a big enough number. But this is not efficient. Is there a way I can create a matrix big enough and scan in these values into a 2d array?
Assuming that each row will contain the same number of columns (should be a requirement for a proper matrix), you should pre-scan the file - just reading the first line. See how many columns it has, then create a sized 2D array that can be populated from an actual read of the entire file.
Use linked lists. Just manually allocate memory while your input from text file is valid. I'd suggest using two int type variables to track your column and row count so you could later easily find entries if you need them by coordinates in your matrix.
Basically you will have a one dimensional array, but you will know that in reality it is two dimensional. Two variables I mentioned earlier will serve so you would know actual dimension of your matrix.
Element you will try to find with coordinates x and y will be located at index:
y * columnCount + x
To learn how the linked lists work please read this:
http://www.cprogramming.com/tutorial/c/lesson15.html
I'm trying to build a board of 4X4 and I'm trying to use the rand function to put random characters in the board.
I need 8 pairs of characters and I don't want to have more than one pair of the same char.
how should I do it?.. I tried a lot of variations without success.
Please help.
Not sure my answer is what you exactly want. I hope it will be helpful.
It looks like your question is more like a algorithm issue. Let's say you are trying to find 8 unique random character pairs and each pair contains two different characters.
Then you can do as following:
Get all possible characters you may use, for instance A ~ Z.
Create one array and its value is a unique character pair which has two characters you want to use. You can use a nested loop to do it.
Record how many elements you have in the array. Assume the value is N.
Use function rand() and number N to get one random number r1.
Pick up the value at position r1 of array and put it into your board.
Switch this element with the last element of array.
Use function rand() and number N-1 to get one random numbe r2. Then do step 5, 6 again.
Do it as step 4 to step 7 to get all 8 pair you want.
If you just want to get 16 unique characters, then just ignore step 2 but keep an array which has all possible characters.
If you want to some weird character, such as '$', '%', etc, then use ASC values.