GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2007, 13:39
rastovacd rastovacd is offline
New Member
 
Join Date: Nov 2007
Posts: 12
rastovacd is on a distinguished road

Do you have a time for a help me


Do you have a time for a help me

I made programm which read and wrote .csv file in two – dim array on dinamic memori (big matrix 1000 x 500). The .csv have “0” and “1” and “,”. That I made two matrix (which both had “-1” on all position), first counter “1” in colums big matrix and wrote this position, second counter “1” in rows big matrix and wrote this position. This is small example :


H matrix

1 0 1 0
1 0 0 0
0 1 1 1 Cmax=3
1 1 1 1 Rmax=4

First matrix for position for colums in big matrix Cmax

0 2 0 2
1 3 2 3
3 -1 3 -1

Counter “1” for colums

3 2 3 2

Second matrix for position for rows in big matrix Rmax

0 0 1 0
2 -1 2 1
-1 -1 3 2
-1 -1 -1 3

Counter “1” for rows

2 1 3 4

My final program isn’t good because include more memory at dinamical space and can’t compile it.
I have idea but I can’t realise it because I need more knowledge.
My idea is that only read big matrix and when to take one element from big matrix synchronous to fill first and secund matrix. Dimension first matrix is [Cmax] x [1000] (Cmax is Counter “1” for colums) and dimension second matrix is [Rmax] x [500] (Rmax is Counter “1” for rows). I think that need one function for counter Cmax and Rmax.

So you have a time pleas send me your e – mail address or make at yahoo.com because your discretion and I send you my program and .csv file because can’t put .csv file at GID Forum

Thank you.
Send me replay message. Do you can help me.
Tell me How do you send .csv file and my programm
  #2  
Old 06-Nov-2007, 14:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Do you have a time for a help me


Quote:
Originally Posted by rastovacd
...

I'm not exactly sure what exactly your assignment is, so I will outline a program to perform my guess as to your task.

If it doesn't help you, then I am sorry...

If all you need to do is to count the number of ones in each column and count the number of ones in each row, then you don't need to store the entire huge array in memory. Just make arrays for the counters and update them as each row is read from the file.


Code:
Suppose you know that your csv file has M lines (rows). Each line has N entries (columns). (Maybe these values are not known at compile time, but must be discovered at run time, so you will need dynamically allocated arrays.) For each row, you want to know how many column entries are equal to 1 For each column, you want to know how many row entries are equal to 1 Make two arrays of ints: /* an array of M ints for the M rows. Each element in the array will hold the * number of '1' entries for that row */ int *onesOnRow = malloc(M * sizeof(int)); /* one element for each row */ /* an array of N ints for the N columns. Each element in the array will hold * the number of '1' entries in that column */ int *onesInCol = malloc(N * sizeof(int)); /* one element for each column */ See footnote. One more array: /* a temporary array that will hold the column entries for each line as it is read *from the file */ int *tempColumns = malloc(N * sizeof(int)); /* one element for each column */ Loop On rownumber from rownumber = zero up to and including rownumber = M-1 Read the next line from the file and get array elements (the N column values) for that line into the tempColumns array. IF there are no more lines in the file THEN break out of the loop ELSE Set counter equal to zero LOOP On j from j = zero up to and including j = N-1 IF tempColumns[j] is equal to 1 THEN increment counter; increment onesInCol[j]; END IF; END LOOP On j Set onesInRow[rownumber] equal to counter END IF END Loop On rownumber

Regards,

Dave

Footnote: Since these are arrays of ints and you might want to make sure that they start with all zeros, maybe calloc() would be a better function than malloc(). If you use malloc, then I think you need a separate loop (or, maybe, you could call setmem()) to make sure that all of the onesInCol[] elements are set equal to zero before beginning the read operations.
Last edited by davekw7x : 06-Nov-2007 at 15:30.
  #3  
Old 06-Nov-2007, 16:24
rastovacd rastovacd is offline
New Member
 
Join Date: Nov 2007
Posts: 12
rastovacd is on a distinguished road

Re: Do you have a time for a help me


Thank you for information and see my code which can't compile because need more memory

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#define DIMV 500
#define DIMK 1000


main () {
	unsigned long int i=0, j=0;
	char far * matrix;
	char element;
	FILE *data;
	int c1max=0;
	int cmax=0;
	int r1max=0;
	int rmax=0;
	int k = 0;
	int l=0;
	int brojac = 0;
	int far * b;
	int far * c;
	int far * d;
	int far * e;
	matrix = (char far *)farmalloc((unsigned long int)DIMV*DIMK*sizeof(char));
	if (matrix == NULL)
		{
			printf("/nMEMORY FULL!!!");
			return 1;
		}

	if ((data = fopen("matrix_H.csv", "r+")) == NULL)
		{
			printf("Haven't got data!!!");
			return 1;
		}
	element = fgetc (data);
	while (element !=EOF){
	 if((element == '1') || (element == '0'))
			{
				*(matrix+i) = element;
				i++;
			}
		element = fgetc (data);
	}

	fclose(data);

	
	/*FIRST MATRIX*/
	/*Counter “1” for colums - cmax*/
	for (j=0; j < DIMK; j++)
	{
		for (i=0; i < DIMV; i++)
		{
			if (*(matrix+i*DIMK+j) == '1')
				c1max++;
		}
		if(c1max >= cmax)
			cmax = c1max;
		c1max=0;
	}
	printf("%d ", cmax);
	printf("\n");
	/*PUT "-1" AT ALL ELEMENT IN FIRST MATRIX AND PUT POSITION FROM COLUMS*/

	b = (int far *)farmalloc((unsigned long int)cmax*DIMK*sizeof(int));
	if (b == NULL)
		{
			printf("/nMEMORY FULL!!!");
			return 1;
		}

	for (i=0; i < cmax; i++)
		for (j=0; j < DIMK; j++)
			*(b+i*DIMK+j)= -1 ;

	for (j=0; j < DIMK; j++)
	{
		for (i=0; i < DIMV; i++)
			if (*(matrix+i*DIMK+j) =='1')
			{
				*(b+k*DIMK+j) = i;
				k++;
			}
		k=0;
	}

	for (i=0; i < cmax; i++)
	{
		for (j=0; j < DIMK; j++)
		{

		    printf("%d ", *(b+i*DIMK+j));
		}
		printf("\n");
	}

	
	/*ARRAY - WHICH COUNTER HOW HAS '1' IN  COLUMS*/
	c = (int far *)farmalloc(DIMK*sizeof(int));
	if (c == NULL)
		{
			printf("/nMEMORY FULL!!!");
			return 1;
		}

	for (j=0; j < DIMK; j++)
	{
		for (i=0; i < cmax; i++)
		{
			if(*(b+i*DIMK+j)!= -1)
				brojac++;
		}
		*(c+j) = brojac;
		brojac = 0;
	}

	for (j=0; j < DIMK; j++)
	{
		printf("%d ", *(c+j));
	}
	printf("\n");
	scanf("%i", &j);
	/*SECOND MATRIX*/
	/*Counter “1” for rows - rmax*/
	for (i=0; i < DIMV; i++)
	{
		for (j=0; j < DIMK; j++)
		{
			if (*(matrix+i*DIMK+j) == '1')
			r1max++;
		}
		if(r1max >= rmax)
			rmax = r1max;
		r1max=0;
	}
	
	printf("%d ", rmax);
	printf("\n");

	/*PUT "-1" AT ALL ELEMENT IN SECOND MATRIX AND PUT POSITION FROM ROWS*/

	d = (int far *)farmalloc((unsigned long int)rmax*DIMK*sizeof(int));
	if (d == NULL)
		{
			printf("/nMEMORY FULL!!!");
			return 1;
		}

	for (i=0; i < rmax; i++)
		for (j=0; j < DIMV; j++)
			*(d+i*DIMV+j)= -1 ;

	for (i=0; i < DIMV; i++)
	{
		for (j=0; j < DIMK; j++)
			if (*(matrix+i*DIMK+j) == '1')
			{
				*(d+ DIMV*k+l) = j;
				k++;
			}

		k=0;
		l++;
	}

	for (i=0; i < rmax; i++)
	{
		for (j=0; j < DIMV ; j++)
		{

		    printf("%d ", *(d+i*DIMV+j));
		}
		printf("\n");
	}
	

	/*ARRAY - WHICH COUNTER HOW  HAS '1' IN  COLUMS*/
	e = (int far *)farmalloc(DIMV*sizeof(int));
	if (e == NULL)
		{
			printf("/nMEMORY FULL!!!");
			return 1;
		}

	for (j=0; j < DIMV; j++)
	{
		for (i=0; i < rmax; i++)
		{
			if(*(d+i*DIMV+j)!= -1)
				brojac++;
		}
		*(e+j) = brojac;
		brojac = 0;
	}
	for (j=0; j < DIMV; j++)
	{
		printf("%d ", *(e+j));
	}
	printf("\n");
	farfree(matrix);
	farfree(b);
	farfree(c);
	farfree(d);
	farfree(e);
	scanf("%i", &element);

	return 0;
}
Last edited by admin : 06-Nov-2007 at 19:32. Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
  #4  
Old 06-Nov-2007, 17:56
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,004
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Do you have a time for a help me


Quote:
see my code which can't compile because need more memory
Very good.
NOW we can see EXACTLY what you are trying to do. Thanks!
CPP / C++ / C Code:
    /* But next time use the c++ tags to enclose your code. 
       See the directions for doing that at the top of the forum listings.
       And take some time to read the Guidelines. */
Have some patience while we look your code over and come up with some ideas for you.
Last edited by Howard_L : 06-Nov-2007 at 18:35.
  #5  
Old 06-Nov-2007, 18:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Do you have a time for a help me


Quote:
Originally Posted by rastovacd
Thank you for information and see my code which can't compile because need more memory

In the other thread you said that you were using Visual Studio Version 6. Here's how:

1. Don't try to include <alloc.h>. It isn't there.

2. You must change every place where the word "far" appears. There is no more "far" stuff.

3. You should get rid of all of those ugly, unnecessary casts in the malloc() statements. It's not "wrong" to put them there, but they really clutter up the scene.)


CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define DIMV 500
#define DIMK 1000

int main()
{
    int i = 0;
    int j = 0;
    char *matrix;
    char element;
    FILE *data;
    int c1max = 0;
    int cmax = 0;
    int r1max = 0;
    int rmax = 0;
    int k = 0;
    int l = 0;
    int brojac = 0;
    int *b;
    int *c;
    int *d;
    int *e;
    matrix = malloc(DIMV * DIMK ); /* sizeof(char) is always 1 */
    if (matrix == NULL) {
        printf("/nMEMORY FULL!!!");
        return 1;
    }

    if ((data = fopen("matrix_H.csv", "r+")) == NULL) {
        printf("Haven't got data!!!");
        return 1;
    }
    element = fgetc(data);
    while (element != EOF) {
        if ((element == '1') || (element == '0')) {
            *(matrix + i) = element;
            i++;
        }
        element = fgetc(data);
    }

    fclose(data);


/*FIRST MATRIX*/
/*Counter “1” for colums - cmax*/
    for (j = 0; j < DIMK; j++) {
        for (i = 0; i < DIMV; i++) {
            if (*(matrix + i * DIMK + j) == '1')
                c1max++;
        }
        if (c1max >= cmax)
            cmax = c1max;
        c1max = 0;
    }
    printf("%d ", cmax);
    printf("\n");
/*PUT "-1" AT ALL ELEMENT IN FIRST MATRIX AND PUT POSITION FROM COLUMS*/

    b = malloc(cmax * DIMK * sizeof(int));
    if (b == NULL) {
        printf("/nMEMORY FULL!!!");
        return 1;
    }

    for (i = 0; i < cmax; i++)
        for (j = 0; j < DIMK; j++)
            *(b + i * DIMK + j) = -1;

    for (j = 0; j < DIMK; j++) {
        for (i = 0; i < DIMV; i++)
            if (*(matrix + i * DIMK + j) == '1') {
                *(b + k * DIMK + j) = i;
                k++;
            }
        k = 0;
    }

    for (i = 0; i < cmax; i++) {
        for (j = 0; j < DIMK; j++) {

            printf("%d ", *(b + i * DIMK + j));
        }
        printf("\n");
    }


/*ARRAY - WHICH COUNTER HOW HAS '1' IN COLUMS*/
    c = malloc(DIMK * sizeof(int));
    if (c == NULL) {
        printf("/nMEMORY FULL!!!");
        return 1;
    }

    for (j = 0; j < DIMK; j++) {
        for (i = 0; i < cmax; i++) {
            if (*(b + i * DIMK + j) != -1)
                brojac++;
        }
        *(c + j) = brojac;
        brojac = 0;
    }

    for (j = 0; j < DIMK; j++) {
        printf("%d ", *(c + j));
    }
    printf("\n");
    scanf("%i", &j);
/*SECOND MATRIX*/
/*Counter “1” for rows - rmax*/
    for (i = 0; i < DIMV; i++) {
        for (j = 0; j < DIMK; j++) {
            if (*(matrix + i * DIMK + j) == '1')
                r1max++;
        }
        if (r1max >= rmax)
            rmax = r1max;
        r1max = 0;
    }

    printf("%d ", rmax);
    printf("\n");

/*PUT "-1" AT ALL ELEMENT IN SECOND MATRIX AND PUT POSITION FROM ROWS*/

    d = malloc(rmax * DIMK * sizeof(int));
    if (d == NULL) {
        printf("/nMEMORY FULL!!!");
        return 1;
    }

    for (i = 0; i < rmax; i++)
        for (j = 0; j < DIMV; j++)
            *(d + i * DIMV + j) = -1;

    for (i = 0; i < DIMV; i++) {
        for (j = 0; j < DIMK; j++)
            if (*(matrix + i * DIMK + j) == '1') {
                *(d + DIMV * k + l) = j;
                k++;
            }

        k = 0;
        l++;
    }

    for (i = 0; i < rmax; i++) {
        for (j = 0; j < DIMV; j++) {

            printf("%d ", *(d + i * DIMV + j));
        }
        printf("\n");
    }


/*ARRAY - WHICH COUNTER HOW HAS '1' IN COLUMS*/
    e = malloc(DIMV * sizeof(int));
    if (e == NULL) {
        printf("/nMEMORY FULL!!!");
        return 1;
    }

    for (j = 0; j < DIMV; j++) {
        for (i = 0; i < rmax; i++) {
            if (*(d + i * DIMV + j) != -1)
                brojac++;
        }
        *(e + j) = brojac;
        brojac = 0;
    }
    for (j = 0; j < DIMV; j++) {
        printf("%d ", *(e + j));
    }
    printf("\n");
    free(matrix);
    free(b);
    free(c);
    free(d);
    free(e);
    scanf("%c", &element);

    return 0;
}

This program compiles with Visual C++ version 6. There are numerous other style "tweaks" that I would do if it were mine, but maybe that's clean enough for now.

Now, to test the allocation part without actually running the program with real data, I made a "worst case" test which would happen if all of the matrix values were 1:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define DIMV 500
#define DIMK 1000

int main()
{
    char *matrix;
    int *b;
    int *c;
    int *d;
    int *e;
    int cmax;
    int rmax;
    double total_bytes;
    /* 1: matrix is  500*1000 chars */
    matrix = malloc(DIMV * DIMK);
    if (matrix == NULL) {
        printf("1: MEMORY FULL!!!\n");
        return 1;
    }
    total_bytes = DIMV * DIMK;
    printf("Allocated %7d bytes for matrix\n", DIMV*DIMK);

    /* 
     * 2: b is cmax * DIMK array of ints; cmax coould be 500
     * so this could be asking for 500,000 ints
     */
    cmax = 500;
    b = malloc(cmax * DIMK * sizeof(int)); /* 1000 * cmax ints */
    if (b == NULL) {
        printf("2: MEMORY FULL!!!\n");
        return 1;
    }
    total_bytes += cmax*DIMK*sizeof(int);
    printf("Allocated %7d bytes for b\n", cmax*DIMK*sizeof(int));

    /* 3: c is 1000 ints */
    c = malloc(DIMK * sizeof(int)); /* 1000 ints */
    if (c == NULL) {
        printf("3: MEMORY FULL!!!\n");
        return 1;
    }
    total_bytes += DIMK*sizeof(int);
    printf("Allocated %7d bytes for c\n", DIMK * sizeof(int));

    /* 4: d is 1000*rmax ints rmax could be 1000 */
    rmax = 1000;
    d = malloc(rmax * DIMK * sizeof(int)); /* rmax * 1000  ints */
    if (d == NULL) {
        printf("4: MEMORY FULL!!!\n");
        return 1;
    }
    total_bytes += rmax*DIMK*sizeof(int);
    printf("Allocated %7d bytes for d\n", rmax*DIMK*sizeof(int));

    /* 5: e is 500 ints */
    e = malloc(DIMV * sizeof(int)); /* 500 ints */
    if (e == NULL) {
        printf("5: MEMORY FULL!!!\n");
        return 1;
    }
    total_bytes += DIMV*sizeof(int);
    printf("Allocated %7d bytes for e\n", DIMV*sizeof(int));

    printf("          -------\n");
    printf("Allocated %7.0f bytes in all\n", total_bytes);

    free(matrix);
    free(b);
    free(c);
    free(d);
    free(e);

    return 0;
}

Results after compiling with Visual C++ version 6.0 on my Windows XP workstation:
Code:
Allocated 500000 bytes for matrix Allocated 2000000 bytes for b Allocated 4000 bytes for c Allocated 4000000 bytes for d Allocated 2000 bytes for e ------- Allocated 6506000 bytes in all

I haven't tried to analyze the program design or implementation (and I won't), but at least you should be able to get it to compile with Visual C++ so that you can test/debug it.

If you have any specific questions, ask.

Regards,

Dave

Footnotes:
1. Programs compiled with the old compilers (with the "far" data types) for the old operating systems could only access about a megabyte of memory. Your program requires much more than that in data memory. Even with modern compilers and operating systems you must learn to be aware of your program's demands on memory and other resources.

2. I can't believe that you really need to hold all of the data in memory all at the same time. I respectfully suggest that you think about how you can perform the task with less memory. I outlined an approach to counting ones in rows and columns that takes far less memory. If you need to do more, then you may need more, but think about it anyhow.
 
 

Recent GIDBlogCompress Your Web Site by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous transfer question crystalattice Miscellaneous Programming Forum 2 24-Jan-2007 20:39
constructors/classes mapes479 C++ Forum 3 19-Nov-2006 17:34
Fortran problem... Justin Fox Miscellaneous Programming Forum 6 24-Oct-2006 15:30
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:17.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.