Help w/ Reading Matrix & Storing in dynamic array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help w/ Reading Matrix & Storing in dynamic array
# 1  
Old 04-05-2014
Help w/ Reading Matrix & Storing in dynamic array

First of I would just like to state that I am not looking for you guys to just do my work for me, I do want to learn and actually understand everything that is happening.

Hey all, I am having trouble on this. What I need to do is... Write an executable C file that will take a text file (not a specific file, will be tested with many) that contains a NxN matrix and read it into a dynamically allocated array so that I can manipulate it however I would like.
I am fairly new to Unix and C and am having trouble with writing the code out.
I understand the steps needed by my program:
1. Use a loop to Scan or get (idk correct terminology) # of rows and columns in the test file.
2. Allocate the proper amount of memory (using malloc) based on the number of rows and columns.
3. Once memory is allocated, create 2d array of the needed size.
4. Loop through and store everything in the test file into the array.

I guess I am just not familiar enough with writing my own code in this language, and also doesn't help that I am doing this through a putty Debian server (not noob friendly IMO). Anything I write I usually end up just scrapping an hour after because I feel I have gotten nowhere with it and end up just overwhelmed by the problem at hand.

So would anyone mind helping me out in any way possible? I am willing to do whatever it takes to get a grasp on this stuff, I need to know this so if anyone would perhaps like to skype or something (I feel it would be much much easier that way) please let me know!
# 2  
Old 04-06-2014
Welcome to forum!

You can use search tool as many posts are repeated here, if you can provide us sample input and expected output, its well and good

following post might be useful for you, which reads file and stores in 2d array

https://www.unix.com/302882682-post2.html
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 04-06-2014
Is this a homework assignment? (Special rules apply to homework assignments in these forums.)

We don't do Skype here, everything is done in the forums so all of us can learn from the problems and answers archived here.

Please don't throw away your code! Show us what you have done so we can get an idea of what problems you're having.

The first thing I'd do if I were tackling a problem like this would be to ask a few questions:
  1. Are there headers in these files that specify the number of rows and columns of data present in the file, or are you supposed to determine the dimensions by reading the entire file?
  2. Are there headings on the 1st few rows of these files that describe what is in the underlying columns?
  3. What type of data is in the tables (integers, floating point values, single characters, strings of characters, ...)? What range of values need to be processed? What precision? If integers, signed or unsigned?
  4. What is the format of the data in the files?
  5. What separates elements in rows in the files (comma, single space, one or more spaces, tabs, ...)?
  6. Show us a few lines from the start of some sample text files that you want your program to process.

Last edited by Don Cragun; 04-06-2014 at 04:08 AM.. Reason: Finish thought...
# 4  
Old 04-06-2014
Quote:
Originally Posted by Don Cragun
  1. Are there headers in these files that specify the number of rows and columns of data present in the file, or are you supposed to determine the dimensions by reading the entire file?
  2. Are there headings on the 1st few rows of these files that describe what is in the underlying columns?
  3. What type of data is in the tables (integers, floating point values, single characters, strings of characters, ...)? What range of values need to be processed? What precision? If integers, signed or unsigned?
  4. What is the format of the data in the files?
  5. What separates elements in rows in the files (comma, single space, one or more spaces, tabs, ...)?
  6. Show us a few lines from the start of some sample text files that you want your program to process.
1. No headers will be present, dimensions must be determined by reading the entire file.
2. No headings to label columns, the only text in the file will be the matrix itself.
3. They will be ints, range should be something like -100 to 100.
4. I'm not sure what you mean by this?
5. Single spaces will be the separator.
6. Something as simple as:
Code:
10 20 30 40
50 60 70 80
90 100 10 20

should work just fine.
This User Gave Thanks to innvert For This Post:
# 5  
Old 04-06-2014
Is this a homework assignment? If not, how will this software be used?

Now that we know that you're processing ints, "What is the format of the data in the files?" is asking if all of the values can be assumed to be decimal values with no "leading" zeroes? If there are leading zeros on input, should the value still be treated as decimal, or can it be treated as octal? Could there be hexadecimal values (with a leading 0x or 0X)?

Please show us your latest attempt at a solution!
# 6  
Old 04-06-2014
Here is what I have right now.

Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
  int n;
  int i=0;
  int j=0;
  scanf("%d", &n);
  int **array;
  array = malloc(n * sizeof(int *));

  if(array == NULL) {
    printf("Out of memory\n");
    exit(1);
  }

  for(i = 0; i < n; i++) {
    array[i] = malloc(n * sizeof(int));
    if(array[i] == NULL) {
      printf("Out of memory\n");
      exit(1);
    }

  }

  for(i = 0; i < n; i++) {
    for(j = 0; j < n; j++) {
      int k;
      scanf("%d", &k);
      array[i][j] = k;
      printf("%d ", array[i][j]);
    }
  }
}

When I run this on a test file (such as one shown above) it give me output:
Code:
20 30 40 50 60 70 80 90 100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20.....repeating 20s

At this point, I am just trying to print out the matrix itself, so whatever is in the test file is what I would like to output, like so.
Code:
10 20 30 40
50 60 70 80
90 100 10 20

This tells me that either A. Something is wrong with how I am reading it into the array, or B. my print method is wrong.
Can anyone lead me in the right direction?
This User Gave Thanks to innvert For This Post:
# 7  
Old 04-06-2014
For the last time: Is this a homework assignment? If not, how will this software be used?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to output the array result into a matrix

I have a file like this: ASSPASVFETQY,hTRBV12-4,hTRBJ2-5,2 ASSPASTGGDYGYT,hTRBV18,hTRBJ1-2,2 ASSPASGDGYT,hTRBV5-1,hTRBJ1-2,2 ASSPASFPEDTQY,hTRBV27,hTRBJ2-3,2 ASSPARVNYGYT,hTRBV5-1,hTRBJ1-2,2 ASSPARTSGGLNEQF,hTRBV6-4,hTRBJ2-1,2 ASSPARQSYNEQF,hTRBV11-1,hTRBJ2-1,2... (3 Replies)
Discussion started by: xshang
3 Replies

2. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

3. Programming

Reading Scientific notation from file and storing in array

Hi, I am trying to read a set of numbers that are in scientific notation into a file so I can do some math on them, but when I display the array contents the numbers aren't the same as the numbers in the file. Could someone explain why? Thanks. int main() { double fArray; ... (3 Replies)
Discussion started by: Filter500
3 Replies

4. Shell Programming and Scripting

Extracting columns from a matrix and storing each column in a separate file

Hi All, I have a huge matrix file consisting some some millions rows and 6000 columns. The contents are just floating point numbers in the matrix. I want to extract each column (i.e. 6000 of them) and store each column in a separate file. For example, 1.dat will consist of elements from column... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

5. Shell Programming and Scripting

Reading from a file and storing it in a variable

Hi folks, I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it. My file is 1.txt and its contents are VERSION=5.6 UPDATE=4 I would like to read "5.6" and "4" and store it in a variable in shell... (6 Replies)
Discussion started by: scriptfriend
6 Replies

6. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

7. Shell Programming and Scripting

storing records in awk array

hi i have a file as follows: 1 2 3 4 5 6 i want to store all these numbers in an array using awk.. so far i have: awk '{for(i=1;i<=NR;i++) {a=$1}} END {for(i=1;i<=NR;i++) {printf("%1.11f",a)}}' 1.csv > test however, i am getting all values as zero in the "test" file..... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

9. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

10. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question