How to make number grid - grid

i want help with making a grid that have 10 rows across the screen and 20 down the screen using while and other loops.
The number range is from 1 - 50 this is my process so far
i tried doing this and it will print numbers from 1 to 50 down ths screen
this is my process so far:
for (int num=1; num <=50; num++) {
System.out.println(num);
}

Something like this?
String retval= "";
for (int num=1; num <=50; num++) {
retval +=num+"|";
if(num%10 == 0){
System.out.println(retval);
retval = "";
}
}

Related

Vector out of range for elements of different size

I'm trying to add items from a csv into a vector. Some of the items have 2 elements, some 3, some 4. The code is working fine for the first 3 elements, but when I try to add the fourth I get an out of range error.
int i = 1;
while (i < temp.size()) {
Course course;
course.courseId = temp[i][0];
course.name = temp[i][1];
if (!temp[i][2].empty()) {
course.prereq = temp[i][2];
/*if (!temp[i][3].empty()) {
course.prereq2 = temp[i][3];
}*/
}
courses.push_back(course);
i++;
cout << i;
}
Thank you.

Infinite loop (do - while)

for(int m=0;m<=3;m++){
for(int n=0;n<=3;n++){
if(n>0){
int c =n,t=1;
do{
t = up_key_no0(&puzz[c][m]);
c--;
}while(t==1||c>=0);
}
}
}
int up_key_no0(int *puzy){
int *puzx = puzy -4;
int down = *puzy;
int up = *puzx;
if(((down==up)||(up==0))&&down!=0){
*puzx += *puzy;
*puzy=0;
return 1;
}
else{
return 0;
}
}
Is The Following piece of code wrong? if Yes Then Reply. The Whole Code Cant Be Fit But puzz is a 2 dimensional array of 4X4
Your do-while loop can go out of the range of the table to the negative indices when the t is 1 and the c is 0. So maybe you should change the condition to (t == 1 && c >= 0) (and instead of or).
I don't know that language is this, but case it's like Java, a "for" should be like so:
for (var i=0;i<=3;i++) {
}
Your while maybe wrong. This "==" on the while should be "=".
while(t=1||c>=0) {
}

Turbo C++ 2D array allocation

enter code here![I'm using Turbo c++....whenever I assign values for a 2d array and try to display them....atleast one of the rows (or all of them) of the array won't be allocated with the proper values the user has entered. I ignored this because when the program was made to run for the 2nd time, it worked fine! But now array allocation itself isnt working properly. compiler error?
PROGRAM.... i've entered 5 rows and 2 column values....
for eg.
1 2
2 5
3 6
5 8
4 7
the above are inputs...
the output should be same as well...but it shows...
1 2
2 5
4 7
4 7
4 7
p.s. I know only to work with Turbo c++...so please dont suggest Dev c++
As a newbie, I could use some help. thanks!
THE CODE IS AS FOLLOWS
` #include
#include
void main()
{
float **arr;
cout<<"rows : ";
cin>>SIZE;
cout<<"col : ";
cin>>n;
arr=new float *[SIZE];
for(int Di=0;Di<n;Di++)
{
arr[Di]=new float[n];
}
cout<<"enter...";
for(int i=0;i<(SIZE);i++)
{
cout<<"\n";
for(int j=0;j<n;j++)
{
cout<<"\t";
cin>>arr[i][j];
}
}
for(int ii=0;ii<SIZE;ii++)
{
cout<<"\n";
for(int jj=0;jj<n;jj++)
{
cout<<"\t";
cout<<arr[ii][jj];
}
}
getch();
}`
The program allocates n rows instead of SIZE rows.
You want the loop
for (int Di = 0; Di < n; Di++)
to read
for (int Di = 0; Di < SIZE; Di++)

how to make a map in xna 4 with matrix from text file

I am trying to make a map by reading a text file line by line (because i cant find how to do that word by word). So I make a map00.txt that looks like "33000000111" (every number is one row, first 2 rows are number of columns and rows so matrix that I load it into looks like
000
000
111
). Now I am supposed to draw 3 tiles at the bottom (1=draw tile). I do so by drawing tile at its position in matrix * window height(width) / matrix number of rows(columns).
PROBLEM: i cant get the right parameters for current window width and height.
Code for loading tiles:
public int[,] LoadMatrix(string path)
{
StreamReader sr = new StreamReader(path);
int[,] a = new int[int.Parse(sr.ReadLine().ToString()),
int.Parse(sr.ReadLine().ToString())];
for(int i = 0; i < a.GetLength(0); i++)
for (int j = 0; j < a.GetLength(1); j++)
{ a[i, j] =int.Parse(sr.ReadLine().ToString()); }
sr.Close();
return a;
}
Code for drawing tiles:
public void DrawTiles(SpriteBatch sp, GraphicsDeviceManager gdm)
{
for(int i = 0; i < matrix.GetLength(0); i++)
for(int j = 0; j < matrix.GetLength(1); j++)
{
if (i == 1)
{
sp.Draw(tile,
new Rectangle(j * (gdm.PreferredBackBufferWidth / 3),//matrix.GetLength(1),
i * (gdm.PreferredBackBufferWidth / 3),//matrix.GetLength(0),
gdm.PreferredBackBufferWidth / matrix.GetLength(1),
gdm.PreferredBackBufferHeight / matrix.GetLength(0)),
Color.White);
}
}
}
but the result is that they are drawn about 40 pixels above the bottom of the screen!
and i tried with GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height(Width) but i get the same result. And when i put calculated numbers that should (in theory) be width/columns and heigth/rows i get what i want. So any suggestions would be VERY appriciated because i am stuck at this for a long time on google and Stack Overflow.
Here is a reworked version of your Draw code, which should work:
public void DrawTiles(SpriteBatch sp, GraphicsDeviceManager gdm)
{
//You would typically pre-compute these in a load function
int tileWidth = gdm.PreferredBackBufferWidth / matrix.GetLength(0);
int tileHeight = gdm.PreferredBackBufferWidth / matrix.GetLength(1);
//Loop through all tiles
for(int i = 0; i < matrix.GetLength(0); i++)
{
for(int j = 0; j < matrix.GetLength(1); j++)
{
//If tile value is not 0
if (matrix[i,j] != 0)
{
sp.Draw(tile, new Rectangle(i * tileWidth, j * tileHeight, tileWidth, tileHeight), Color.White);
}
}
}
}

How to display single column data in tabular format?

I want to display items as follows,
<td1> <td2> <td3> <td4>
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18
I am getting data (from 1......23) in a single column from database. Now I want to run a loop which will display my single columns data in the above format.
Please tell me the for loop code using which I can display data in above format. Data can be more that 23 in production environment, So the logic should be as that it can handle any amount of data. I am using ASP.NET(C#).
Thanks
I think you could use asp:DataList and bind the data to it.
Here is an example of using datalist with RepeatDirection and RepeatColumns properties.
OK, here's a corrected version of Riho's loop:
int records = ... ; /* the number of records */
int cols = 4; /* the number of columns */
int rows = (records + cols - 1) / cols; /* nb: assumes integer math */
for (int row = 0; row < rows; ++row) {
print "<tr>";
for (int col = 0; col < cols; ++col) {
print "<td>";
int offset = col * rows + row;
if (offset < records) {
print data[offset];
} else {
print "nbsp;" /* nb: should have an & but markdown doesn't work */
}
print "</td>";
}
print "</tr>";
}
The cell is often necessary to ensure that the rendered HTML cell has the correct background. Missing cells or cells with no data in them aren't rendered the same as normal cells.
In pseudocode (didn't test it):
int recordnum=....; //get the total number of records
int col_length=recordnum/4;
for(int i=0;i<col_length;i++)
{ for(int j=0;j<4;j++)
print data[i+j*col_length] ;
print "\n";
}

Resources