[SOLVED]Printing 2D array

Need help printing out a 2d array in C

Page 1 of 1

3 Replies - 18861 Views - Last Post: 23 October 2007 - 10:06 PM Rate Topic: -----

#1 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

[SOLVED]Printing 2D array

Post icon  Posted 23 October 2007 - 08:54 PM

Hi everyone,

For some reason I am retarded at arrays and I can't figure out how to print a 2D array in C. I am randomly creating a 0 or 1 and then assigning a blank space for a 0 and a * for a 1. Then I am trying to print out the array (using spaces and stars) but I cannot get it to output correctly.


I would also like to get it to print out in a 20x20 grid. I'm not sure how to do that (I assume with the mod operator?).

As a disclaimer, I have searched this site for a while now and most of the examples are in C++. The C examples haven't helped me :(

Anyway, here is my code, and thanks in advance for any/all help!

char life [max][max];
	char symbol, a, b;
	int r, i, j;

	/* assign random 0 or 1 and convert to " " or "*" for every array address */
	srand(time(NULL));
	for (i = 0; i < max; i++)
	{
		for (j = 0; j < max; j++)
		{
			r = rand()%2;
			if (r == 0)
			{
				symbol = ' ';
			}
			else
			{
				symbol = '*';
			}
			a = symbol;
			b = symbol;
			printf ("%c", life[a][b]);
		}
	}


This post has been edited by ShotokanDeity: 23 October 2007 - 09:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED]Printing 2D array

#2 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

Re: [SOLVED]Printing 2D array

Posted 23 October 2007 - 09:32 PM

Ok I figured it out finally. Here is my modified code snippet!

	char life [max][max];
	char symbol;
	int r, i, j;

	/* assign random 0 or 1 and convert to " " or "*" for every array address */

	srand(time(NULL));
	for (i = 0; i < max; i++)
	{
		for (j = 0; j < max; j++)
		{
			r = rand()%2;
			if (r == 0)
			{
				symbol = ' ';
			}
			else
			{
				symbol = '*';
			}
			printf ("%c", symbol, (life[i][j]));
			if (j == 19){printf("\n");}
		}
	}


This post has been edited by ShotokanDeity: 23 October 2007 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: [SOLVED]Printing 2D array

Posted 23 October 2007 - 09:53 PM

I don't think that your code is doing what you want it to. You are in fact printing out a random pattern of symbols, but you aren't assigning them to the array at any point - the argument life[i][j] in your printf statement is extraneous and doesn't do anything in the code. And only printing out the '\n' when the column is 19 only makes sense when max=20.

The following might be a little closer to what you're trying to do:
#include "stdlib.h"
#include "stdio.h"
#include "time.h"

#define max 10

int main() {

	char life [max][max];
	char symbol;
	int r, i, j;

	/* assign random 0 or 1 and convert to " " or "*" for every array address */

	srand(time(NULL));
	for (i = 0; i < max; i++) {
		for (j = 0; j < max; j++) {
			r = rand()%2;
			if (r == 0) {
				symbol = ' ';
			} else {
				symbol = '*';
			}
			life[i][j]=symbol;
			printf ("%c", life[i][j]);
		}
		printf("\n");
	}

	return 0;
}

This gives symbol the value of either '*' or ' ' depending on the value of the random number, assigns the value of symbol to life[i][j], and then prints that array element. It also prints a newline after the inner loop has completed i.e. after every row.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#4 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

Re: [SOLVED]Printing 2D array

Posted 23 October 2007 - 10:06 PM

OK thanks jjhaag. I didn't realize I wasn't actually assigning my array! Your suggestion also made it cleaner.

So here is my new code, and as you can see with all the added stuff I still have a lot to do on this little program (and its due Friday ^_^ hehe).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define max 20

int main(void)
{
	char life [max][max];
	char symbol;
	int r, i, j, evolution=0, blank=0, star=0;

	/* Assign random 0 or 1 and convert to " " or "*" to create initial array. */
	srand(time(NULL));
	for (i = 0; i < max; i++)
	{
		for (j = 0; j < max; j++)
		{
			r = rand()%2;
			if (r == 0)
			{
				symbol = ' ';
				blank = blank + 1;
			}
			else
			{
				symbol = '*';
				star = star + 1;
			}
			life[i][j]=symbol;
			printf ("%c", life[i][j]);
		}
		printf("\n");
	}
	printf("Blanks: %i \n", blank);
	printf("Stars: %i \n", star);
	printf("Press 'ENTER' to continue\n");
	fflush(stdout);
	(void)getchar();

	//start loop for 'evolution' to occur 10 times


	//scan each address to decide whether or not an organism is born.

	//scan each address to decide which organisms survive.

	//scan each address to decide which organisms die.

	//print the new array.

	puts("Normal Termination.");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1