2 Replies - 267 Views - Last Post: 02 April 2013 - 07:28 PM Rate Topic: -----

#1 dsmeltaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Storing values in 2D arrays? (C)

Posted 02 April 2013 - 06:07 PM

I am working on a final project for my dual credit class, and I chose to do a simple battleship-like program. Except it it for one person. I will have 9 preset grids with characters representing "ships." I will generate a random integer, run a switch statement to determing which text file to load. No problem there. However, when I read in a file, I want to store values into a 2D array..Would I have to nest loops? Maybe like this? I hope this isn't too ambiguous. I can upload my whole code if preferred.

for (i = 0;i > 10;i++)
{
    for (j = 0;j > 10;j++)
        {
        scanf("%c",&column[j]);
        }
    scanf("%c\n",&row[i]);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Storing values in 2D arrays? (C)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,401
  • Joined: 19-February 09

Re: Storing values in 2D arrays? (C)

Posted 02 April 2013 - 06:50 PM

Nested loops yes, two reads (scanf) no.

for each row
  for each col
    myarray[row][col] = read character




You could use fgetc to read a character.
Was This Post Helpful? 0
  • +
  • -

#3 dsmeltaway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Storing values in 2D arrays? (C)

Posted 02 April 2013 - 07:28 PM

	for (i = 0;i < 10;i++)
	{
		for (j = 0;j < 10;j++)
		{
			fscanf(inp,"%c",&point[j][i]);
		}
		fscanf(inp,"%c\n",&point[j][i]);
	}


so this is what I used, and it worked like a charm! thank you very much :)/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1