Solved

Battleship-like C, Finale.

Posted on 2004-08-14
6
381 Views
Last Modified: 2008-02-01
Okay, so here is the link to see what part 1 and 2 of this battleship-like C program should do: http://people.cs.uchicago.edu/~salman/
Here is the link as to what the output should be: http://people.cs.uchicago.edu/~salman/output.txt
and last but not least, here is my sourcecode:
http://people.cs.uchicago.edu/~salman/asgn8.c

I have a good amount of it done. If there is anything you can pinpoint, that would be great.

I am using GCC Compiler. I am getting some compile error(invalid type argument of "unary *" ) and it lies in the countpieces() function. If you can, please read through the definitions as well as the output. Please compile and run it to see what you get and any solutions you might see fit for it.. I appreciate your assistance very much.

Im just really exhausted and stomped. It is 2 in the morning and I will get to this at hopefully 9am...Sunday(Central US).
SalmanF.
0
Comment
Question by:simmah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 200 total points
ID: 11803343
int countPieces(int rows, int columns, int * boardPtr, int counts[])
{
  int totalPieces=0;
  int *value[rows];
  int valueNum;
  int i;
  int j;

  for (i=0; i<columns; i++)
  {
    for (j=0; j<rows; j++)
    {
      value[i] = *boardPtr[i];

=========================

 boardPtr  is a pointer to an int , to which I am ssuming you are pasisng the pointer to the its int of an array

value[i] is also an pointer to an int .. but if to value[i] you want to pass the poointer to the ith element of the array pointed to by boardPtr you can do

value[i] = boardPtr +i ;  instead of
value[i] = *boardPtr[i];


http://pweb.netcom.com/~tjensen/ptr/pointers.htm


0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11803782
What does CountPieces do?Does it tell you how many ship spaces you've got remaining?
I mean if i place a ship 1's from A1 to A3,and i've hit A1,countPieces will tell me that i have 2 pieces remaining,Right?

In that case,you can simply traverse your 2d array to find that out.

int countPieces(int rows, int columns, int ** boardPtr)
{
 int totalpieces=0;
 for (i=0; i<rows; i++)
 {
   for (j=0; j<columns; j++)
   {
    if(boardptr[i][j]!=0 && boardptr[i][j]!='x') totalpieces++;
   }
 }
 return totalpieces;
}

Is there a reason why you're passing your 2d array as a 1d array?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11803823
Also,You shouldnt be adding to the counts array every time.

Right now,whats happening is:
Initially counts[] is all 0's.

Every time countpieces() is called,counts[] elements value just keeps getting added up.

This is what i suggest.

At the time of entering the ships,i.e. in the paintspace() function,if its a valid entry,add to the corresponding element in counts[] starting from 0.

Then,ONLY when a HIT happens,decrement the corresponding element of counts[] by 1.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
ankuratvb earned 300 total points
ID: 11803836
Just went thru your first link,and since this is an assignment,you wouldnt be allowed to change the prototype,right?

First suggestion,read the contents of your first link once again.Cos it says very clearly,

>Be sure to initialize the counts array to zero counts at the beginning.

for the countpieces() function

Second suggestion,
int countPieces(int rows, int columns, int *boardPtr,int counts[])
{
int totalpieces=0;
//set counts[] to all 0's.You can do that im sure.

for (i=0; i<rows*columns; i++)
   if(boardptr[i]!=0 && boardptr[i]!='x')
    {
      totalpieces++;
      counts[boardptr[i]-1]++;
    }
  }
}
return totalpieces;
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11803890
>>
BONUS: 20 bonus points will be awarded if you modify your main() routine to handle variable size boards. For this, you will need to use either malloc() or calloc(), and ask the user to input the board size at the begining of the program. Board sizes should not exceed 26x26, nor be smaller than 2x2.
<<

Looks like you're after the bonus points as well.

In your code,

int rows=10,columns=10;
int boardptr[rows][columns];

This is not part of Standard C,its a gcc extension ,thats why you can get it to compile.
To do that in a way so that it runs on all compilers,you'll have to use malloc() or calloc() as the description says.

Have a look here for 2D array Dynamic allocation:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20792387.html
0
 

Author Comment

by:simmah
ID: 11805220
Thanks for your help. Will let you know if I run into anything else.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question