I was wondering how can i make a c program that only prints the black positions on a chess board for example:
- = (empty space);
-| A8 - C8 - E8 - G8 -
-| - B7 - D7 - F7 - H7
-| A6 - C6 - E6 - G6 -
-| - B5 - D5 - F5 - H5
-| A4 - C4 - E4 - G4 -
-| - B3 - D3 - F3 - H3
-| A2 - C2 - E2 - G2 -
-| - B1 - D1 - F1 - H1
#include <stdio.h>
int main()
{
int n = 8;
int i,j;
char a[100][100] = {
"A8"," ","C8"," ","E8"," ","G8",
" ","B7"," ","D7"," ","F8"," ","H7",
"A6"," ","C6"," ","E6"," ","G6",
" ","B5"," ","D5"," ","F5"," ","H5",
"A4"," ","C4"," ","E4"," ","G4",
" ","B3"," ","D3"," ","F3"," ","H3",
"A2"," ","C2"," ","E2"," ","G2",
" ","B1"," ","D1"," ","F1"," ","H1",
};
for(i = 0;i < n;i++){
for(j = 0;j < n;j++){
printf("%c ",a[i][j]);
}
printf("\n");
}
return 0;
}
There is absolutely no reason to use a 2D array here.
Representation on the screen and memory layout of data structures don't need to match. As an example:
#include <stdio.h>
int main(void)
{
unsigned int i;
for (i = 0; i < 64; i++) {
if ((((i % 8) + (i / 8)) & 1) == 0) {
printf("%c%u ", 'A' + (i % 8), 8 - (i / 8));
} else {
printf(" ");
}
if ((i % 8) == 7) {
printf("\n");
}
}
return 0;
}
This code isn't pretty but it works. It doesn't even use an array.
All you need is some operations like division and modulus to determine row and column. Then you need to notice that squares with coordinates (X,Y) share the same color if X+Y is even.
The code isn't pretty, but it uses very simple logic. As an exercise, within the for loop, try to get the coordinates X and Y into separate variables. Then it might be easier to understand.
Please read the comments marked with // CHANGE HERE.
char a[100][100] creates 100 character strings each of max length 99 (1 character for null terminator '\0' (Imagine 100 rows with 99 columns)
#include <stdio.h>
int main()
{
//int n = 8; // CHANGE HERE - unused
int i;
// CHANGE HERE
// 1. Replaced ' ' with ' ' (two spaces)
// 2. Added ' ' after G for white H column cells
char a[100][100] = {
"A8"," ","C8"," ","E8"," ","G8"," ",
" ","B7"," ","D7"," ","F8"," ","H7",
"A6"," ","C6"," ","E6"," ","G6"," ",
" ","B5"," ","D5"," ","F5"," ","H5",
"A4"," ","C4"," ","E4"," ","G4"," ",
" ","B3"," ","D3"," ","F3"," ","H3",
"A2"," ","C2"," ","E2"," ","G2"," ",
" ","B1"," ","D1"," ","F1"," ","H1",
};
// CHANGE HERE - chess board has 64 cells
for (i = 0; i < 64; i++) {
// CHANGE HERE - print a new line after every 8 entries
if (i != 0 && i % 8 == 0)
{
printf("\n");
}
printf("%s", a[i]);
}
printf("\n");
return 0;
}
ihave to read from file and add student name and id to array of object,after i
added it i want sort indexs (information by id)
and this is the input example
100
ADD 123 aa bb
ADD 899 cc dd
ADD 222 ff gg
ADD 565 ii hh
ADD 777 kk oo
QUIT
int maxNumStudents = read.nextInt();
Student[] arrayObStudent = new Student[maxNumStudents];
while (read.hasNext()) {
do {
command = read.next();
switch (command) {
case "ADD":
arrayObStudent[Student.getNumStudents()] = new Student();
arrayObStudent[Student.getNumStudents()].setID(read.nextInt());
arrayObStudent[Student.getNumStudents()].setFname(read.next());
arrayObStudent[Student.getNumStudents()].setlName(read.next());
//this will print it without sort System.out.println(arrayObStudent[Student.getNumStudents()].getID() + "\n");
System.out.println(arrayObStudent[Student.getNumStudents()].getFname());
System.out.println(arrayObStudent[Student.getNumStudents()].getlName());
Student.increseStudents(1);
etc..
On every insert you can check the correct position in the array to insert the student.
That's how the array will remain sorted.
int insertStudent (int arrayObStudent[], int firstStudentId, int lastStudentId, Student target)
{
int i = lastStudentId;
while ((i > firstStudentId) && (target.getID() < arrayObStudent[i-1]))
{
arrayObStudent[i] = arrayObStudent[i-1];
i = i - 1;
}
arrayObStudent[i] = target;
return i;
}
Hope this helps.
I need to draw automatically a complete graph. the complete graph will depend on the number of nodes enter by the user. The weight between two nodes is assigned randomly. The method have used is inefficient because to draw a graph of nine nodes, the user has to click on the screen nine times. Moreover to draw an edge between two nodes, the user has to right click at most at two nodes to establish the link. I have tried to look for some online examples of this, but I cannot seem to find anything. This is what I have done so far
CODE
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Label v = new Label();
v.Content = E;
Ellipse cir = new Ellipse();
cir.Width = 16.5;
cir.Height = 16;
circ.Stroke = Brushes.MediumAquamarine;
circ.StrokeThickness = 2;
CanvasMiddle.Children.Add(circ);
Point a = e.GetPosition(circ);
Canvas.SetLeft(circ, a.X-3);
Canvas.SetTop(circle, a.Y+1);
Vertex.Add(i, new Tuple<double, double>(a.X, a.Y));
CanvasMiddle.Children.Add(value);
Point coordinates = e.GetPosition(v);
Canvas.SetLeft(v, coordinates.X-5);
Canvas.SetTop(va, coordinates.Y-5);
f++;
i++;
}
// To generate the edge between two nodes
private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
for (int i = 1; i <= Node.Count; i++)
{
E1 = Node[i].Item1 - 13; a2 = Node[i].Item1 + 13;
G1 = Node[i].Item2 - 13; b2 = Node[i].Item2 + 13;
if ((P >= E1 && P <= G1) && (Q >= E1 && Q <= G1))
{
ID1[j] = i;
j++;
if (j == 2)
{
Line li = new Line();
li.X1 = Vertex[ID1[0]].Item1 + 5;
li.Y1 = Vertex[ID1[0]].Item2 + 5;
li.X2 =(line.X2-13)+ (Vertex[ID1[1]].Item1 + 5);
li.Y2 =(line.Y2) + (Vertex[ID1[1]].Item2 + 5);
li.Stroke = Brushes.MediumAquamarine;
li.StrokeThickness = 2;
Point co1 = e.GetPosition(li);
Canvas.SetLeft(li, co1.X+2.78);
Canvas.SetTop(li, co1.Y+1.85);
CanvasMiddle.Children.Add(li);
j = 0;
}
Images:
Here is the link of the specific graph at hand.
http://i.stack.imgur.com/kJF5K.png
All this rectangles are added in grid and I want after click each of them to change the color to white, but program throwing and exception.
Can i do this with this way or I need to get the position of rectangle a make on his place a new one ?
Rectangle[] rec = new Rectangle[22 * 12];
for( int i = 0; i < 22 * 12; i++){
rec[i] = new Rectangle(32, 32);
rec[i].setStroke(Color.BLACK);
rec[i].setFill( Color.valueOf("#202020") );
rec[i].setStrokeWidth(1);
rec[i].setOnMouseClicked(e -> {
Rectangle r = new Rectangle(32, 32, Color.WHITE);
rec[i].setFill( Color.WHITE); // exception at this line -> i must be final or ...
});
}
As your compile error says, you can't access non-final variables in a lambda expression. You can get around this by putting your rectangle in a different (effectively-final) variable:
Rectangle[] rec = new Rectangle[22 * 12];
for( int i = 0; i < 22 * 12; i++){
Rectangle r = new Rectangle(32, 32);
r.setStroke(Color.BLACK);
r.setFill( Color.valueOf("#202020") );
r.setStrokeWidth(1);
r.setOnMouseClicked(e -> {
r.setFill( Color.WHITE);
});
rec[i] = r ;
}
}
Given an array of 81 elements (meant to represent a 9x9 grid) how can I go over each element, grabbing the three around it and then performing an operation on those, then continuing to the next three of each row, column, or submatrix. Look below or at a sudoku grid to see the layout.
define COL(n) ((n) % 9)
define ROW(n) ((n) / 9)
define SUB(n) ((n / 3) % 9)
For example, I have
int grid[81];
and
int array_x[9], array_y[9], array_s[9];
Since the total 9x9 grid can be split into 9 of the following categories, there are nine elements in each array, and I hope to take the elements of each column (the x axis) in groups of threes, perform
r = ((a = ~a) & (b = ~b)) | ((b | a) & ~c);
// or
r = ((~a & ~b)) | ((~b | ~a) & ~c);
on them, take the three resultant numbers, and perform it on them, and then store it into the array.
If this sounds impossible, sorry, I'd like a different way to do this. Definitely open to suggestions...
Another try:
void applyThingy(int *grid, int xPitch, int yPitch)
{
int row, column;
int *rowPointer = grid;
for(row = 0; row < 3; ++row)
{
int *columnPointer = rowPointer;
for(column = 0; column < 3; ++column)
{
doOperation(columnPointer);
columnPointer += xPitch;
}
rowPointer += yPitch * 9;
}
}
applyThingy(&grid[SUB(n)], 1, 1); // Perform on 3x3 subgrid
applyThingy(&grid[ROW(n)], 1, 0); // Perform on row
applyThingy(&grid[COL(n)], 0, 1); // Perform on column
I'm not sure what you want to do, but is it something like this?:
#define IDX(row, col) ((row)*9+(col))
int m = some_column;
for (int n = 0; n < 9; n += 3) {
a = grid[IDX(n, m)];
b = grid[IDX(n+1, m)];
c = grid[IDX(n+2, m)];
r = ...;
...
}
Also, I'm not sure what you want to do with your operation
r = ((a = ~a) & (b = ~b)) | ((b | a) & ~c);
You're assigning ~a to a, is that what you want? How is a defined, and what are you trying to set it to? What are you trying to achieve?