I have an array in 1D.
data[27]=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
I need to convert this into a 3D array of the form using C:
data[3][3][3]
Can someone help me doing this?
I tried the following code. Not seems to be working:
#include <stdio.h>
int main()
{
int x;
int y;
int z;
int res;
int data;
int byte[] data={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}; // Read 4096 bytes
byte[][][] res = new byte[3][3][3];
for (x = 0 ; x != 3 ; x++) {
for (y = 0 ; y != 3 ; y++) {
for (z = 0 ; z != 3 ; z++) {
res[x][y][z] = data[3*3*x + 3*y + z];
}
}
}
printf("Printing the 3D matrix\n");
for (x = 0 ; x != 16 ; x++) {
for (y = 0 ; y != 16 ; y++) {
for (z = 0 ; z != 16 ; z++) {
printf("%d\t",res[x][y][z]);
printf("\n");
}
}
}
return 0;
}
Your logic seems to be okay. The problem is with the declaration of the 1D and 3D arrays.
1) There id no datatype as byte in C
2) new is not a part of C. You cannot allocate memory using new
Try the following changes for your code to work
int main()
{
int x;
int y;
int z;
int data[] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}; // Read 4096 bytes
int res[3][3][3];
for (x = 0 ; x < 3 ; x++) {
for (y = 0 ; y < 3 ; y++) {
for (z = 0 ; z < 3 ; z++) {
res[x][y][z] = data[3*3*x + 3*y + z];
}
}
}
printf("Printing the 3D matrix\n");
//run the loop till maximum value of x, y & z
for (x = 0 ; x < 3 ; x++) {
for (y = 0 ; y < 3 ; y++) {
for (z = 0 ; z < 3 ; z++) {
printf("%d\t",res[x][y][z]);
printf("\n");
}
}
}
return 0;
}
Related
I want to build a function to compute (x+y) in powers of (i*i)-i+1 for i from 1 to 10. I can't find the missmatch in my code... For x= 0.25 and y = 0.5 the result should be 1.331 but my result is 0.8
#include <stdio.h>
#include <math.h>
double double_bitxy ( double x , double y )
{
double base = (x + y);
int i = 10 ;
double result = 0 , temp = 0 ;
temp = base ;
for (i = 1; i <= 10; i++)
{
int exp = ((i * i) - i + 1) ;
while (exp!= 0)
{
temp = temp * base ;
--exp ;
}
result += temp ;
}
return result ;
}
int main()
{
double x , y ;
printf("enter x\n") ;
scanf("%lf", &x) ;
printf("enter y\n") ;
scanf("%lf", &y) ;
double sum = double_bitxy(x , y);
printf("%lf", sum);
return 0;
}
#include<stdio.h>
int main()
{
int x, y = 0, z = 1, r, i;
scanf("%d",&x);
for ( i = 0 ; i < x ; i++ )
{
if ( i <= 1 )
z = i;
else
{
z = x + y;
x = y;
y = z;
}
printf("%d ",z);
}
return 0;
}
How to output a , between the numbers like this:
0, 1, 1, 2, 3, 5, 8, 13
I'll try hard but doesn't work. Maybe someone want's to help me.
The easiest way is to add additional printf for the ", " string under the condition that it is not the last for loop iteration
#include<stdio.h>
int main()
{
int x, y = 0, z = 1, r, i;
scanf("%d",&x);
for ( i = 0 ; i < x ; i++ )
{
if ( i <= 1 )
z = i;
else
{
z = x + y;
x = y;
y = z;
}
printf("%d",z);
if (i < x - 1) {
printf(", ");
}
}
return 0;
}
You have to additionally use printf(", "); as follows.
#include<stdio.h>
int main()
{
int x, y = 0, z = 1, r, i;
scanf("%d",&x);
for ( i = 0 ; i < x ; i++ )
{
if ( i <= 1 )
z = i;
else
{
z = x + y;
x = y;
y = z;
}
// Add comma if the print number is not first one
// You can also check by last index, but this is simple condition.
if( i != 0 )
{
printf(", ");
}
// Print number
printf("%d", z );
}
return 0;
}
For avoiding an unwanted comma, we have to handle this case according to the provided sample.
#include<stdio.h>
int main()
{
int x, y = 0, z = 1, r, i;
scanf("%d",&x);
for ( i = 0 ; i < x ; i++ )
{
if ( i <= 1 )
z = i;
else
{
z = x + y;
x = y;
y = z;
}
printf("%d",z);
if( i!=x-1 )
printf(", ");
}
return 0;
}
#include<stdio.h>
int main()
{
int x, y = 0, z = 1, r, i;
scanf("%d",&x);
char *separator = ""; /* this is the separator to be used between the numbers, initialized to the empty string */
for ( i = 0 ; i < x ; i++ )
{
if ( i <= 1 )
z = i;
else
{
z = x + y;
x = y;
y = z;
}
printf("%s%d", separator, z); /* take off the space after %d */
separator = ", "; /* after first number, we change the separator to a comma and an space */
}
printf("\n"); /* this is only recommended, to get a new line at the end */
return 0;
}
You can use this approach even if you don't know the kind of the indices or the data values you have to print in the loop.
Anyway, your program has other problems that I don't know if you want commented:
x is not initialized, so if you don't provide a correct number to scanf, you'll get undefined behaviour as it can have any value in the loop.
I think you use x for two purposes, as a limit on the number of fibonacci number to output, and as one of the adding numbers in the sequence, this is not correct. If you input something as 100 you'll not get the fibonacci numbers upto 100 because the x value is overwritten at the first iteration loop. Use something as limit instead.
I've let this points as exercises for you to complete, as you didn't asked about the wrong list that is output.
I have a file called map.txt, that looks like :
xxxxxxxxxxxxxxxxxx
x Ox xx x
x xx xx xxxxxx x
xx x x
x xxxx x x x x
x x x x
x x x x x x
x x x
x x x x x x
x x
xxxxxxxxxxxxxxxxxx
It represents a maze. I have to read this file char by char and store it in a bidimensional array char data [][] but i'm having problems to use fgetc. here is my code:
int readMapFromFile(char *filename) {
FILE *file = fopen(filename, "r");
char element;
printf("reading map from file %s...\n", filename);
int c;
if(file == NULL)
return 1;
int j =0;
int i =0;
while((c = fgetc(file))!= EOF) {
element = (char) c;
if(element == '\n') {
j = 0;
i++;
continue;
} else if(element == 'O') {
player.exitX = j;
player.exitZ = i;
}
data[i][j] = element;
j++;
}
fclose(file);
return 0;
}
It seems to skip the whitespaces and i have no idea how to make it work.
As request i added how i display the data's content:
int i;
int j;
for (i =0 ; i < map.x ; ++i);
{
for(j = 0 ; j < map.z ; ++j) {
printf("%s \n", &data[i][j]);
}
}
Expected output :
xxxxxxxxxxxxxxxxxx
x Ox xx x
x xx xx xxxxxx x
xx x x
x xxxx x x x x
x x x x
x x x x x x
x x x
x x x x x x
x x
xxxxxxxxxxxxxxxxxx
My output :
xxxxxxxxxxxxxxxxxxxOxxxxxxxxxxxxxxxxxxxxxxx
As you can see not the entire maze is saved.
It's not about skipping whitespace. You just never increment j.
The code in the if(element == '\n') statement should be like this:
if(element == '\n'){
j++;
i = 0;
continue;
}
If you had named your variables x and y instead of i and j, you probably would have found out by yourself.
And your map display function is totally wrong, it should be:
int i;
int j;
for (i = 0 ; i < map.x ; ++i) {
for(j = 0 ; j < map.y ; ++j) {
printf("%c", data[i][j]);
}
printf ("\n");
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to write a sudoku solver
I always get a segmentation fault after calling getPossibleElements in solveSudoku.
If I delete this line the error doesnt appear.
My Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUDOKU_X 8
#define SUDOKU_Y 8
#define MAX_FILENAME 50
#define MAX_POSSIBILITIES 8
typedef enum bool {false, true} bool;
void printPossibilities (bool numbers[], const int pos_x, const int pos_y) {
int i = 0;
for (i = 0; i <= MAX_POSSIBILITIES; i++) {
if (numbers[i]) {
printf("%d ", (i+1));
}
}
}
void getPossibleElements (bool numbers[],int a[][SUDOKU_Y], const int pos_x, const int pos_y) {
int x = 0;
int y = 0;
int i = 0;
int j = 0;
int tmp = 0;
for (x = 0; x <= MAX_POSSIBILITIES; x++) {
numbers[x] = true;
}
/* row */
for (x = 0; x <= SUDOKU_X; x++) {
if (a[pos_y][x] > 0) {
printf("ROW->a[%d][%d]\n",pos_y,x);
printf("ROW->%d\n",a[pos_y][x]-1);
numbers[a[pos_y][x]-1] = false;
}
}
/* coloumn */
for (y = 0; y <= SUDOKU_Y; y++) {
if (a[y][pos_x] > 0) {
printf("coloumn->a[%d][%d]\n",y,pos_x);
printf("coloumn->%d\n",a[y][pos_x]-1);
numbers[a[y][pos_x]-1] = false;
}
}
/* field */
if (pos_x <= 2 && pos_y <= 2) {
x = 0;
y = 0;
}
else if (pos_x <= 5 && pos_y <= 2) {
x = 3;
y = 0;
}
else if (pos_x <= 8 && pos_y <= 2) {
x = 6;
y = 0;
}
else if (pos_x <= 2 && pos_y <= 5) {
x = 0;
y = 3;
}
else if (pos_x <= 5 && pos_y <= 5) {
x = 3;
y = 3;
}
else if (pos_x <= 8 && pos_y <= 5) {
x = 6;
y = 3;
}
else if (pos_x <= 2) {
x = 0;
y = 6;
}
else if (pos_x <= 5) {
x = 3;
y = 6;
}
else if (pos_x <= 8) {
x = 6;
y = 6;
}
printf("DB!!! x=%d y=%d\n", x,y);
for (j = y; j < (y+3); j++) {
for (i = x; i < (x+3); i++) {
if (a[j][i] > 0) {
printf("FIELD->a[%d][%d]\n",j,i);
printf("FIELD->%d\n",(a[j][i])-1);
numbers[(a[j][i])-1] = false;
}
}
}
printf("db");
}
void printSudoku (int a[][SUDOKU_Y]) {
int i = 0;
int j = 0;
printf("-------------------------------\n");
for (j = 0; j <= SUDOKU_X; j++)
{
for (i = 0; i <= SUDOKU_Y; i++) {
if (i == 0) {
printf("|");
}
printf(" %d ",a[j][i]);
if (i == 2 || i == 5 || i == 8) {
printf("|");
}
}
printf("\n");
if (j == 2 || j == 5) {
printf("|-----------------------------|\n");
}
}
printf("-------------------------------\n");
}/* printSudoku */
bool solveSudoku (int a [][SUDOKU_Y]) {
bool numbers[MAX_POSSIBILITIES];
int x = 0;
int y = 0;
printSudoku(a);
getPossibleElements(numbers,a,x,y);
printPossibilities(numbers,x,y);
return true;
}
void readFiletoArray (const char * fileName, int a[][SUDOKU_Y])
{
FILE *fp = fopen(fileName,"r");
int i = 0;
int j = 0;
int val0 = 0;
int val1 = 0;
int val2 = 0;
if( fp == NULL ) {
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
while(fscanf(fp, "%d %d %d", &val0, &val1, &val2) > 0) {
a[j][i++] = val0;
a[j][i++] = val1;
a[j][i++] = val2;
if (i >= 8) {
i = 0;
j++;
}
}
fclose(fp);
} /* readFiletoArray */
int main (int argc, char * argv []) {
int a[SUDOKU_X][SUDOKU_Y];
char fileName[MAX_FILENAME];
bool numbers[MAX_POSSIBILITIES];
bool success = false;
if(argc == 2) {
strncpy(fileName, argv[1], MAX_FILENAME-1);
fileName[MAX_FILENAME] = '\0';
}
else {
printf("ERROR: Invalid Parameter\n");
exit(EXIT_FAILURE);
}
readFiletoArray(fileName, a);
success = solveSudoku(a);
printf("DB");
exit(EXIT_SUCCESS);
} /* Main */
sudoku.txt (Program Parameter)
0 5 9 0 4 0 2 0 0
0 1 0 0 5 0 0 0 7
4 0 0 3 2 9 0 1 5
3 2 0 1 0 0 9 0 0
0 0 7 4 0 6 5 0 0
0 0 4 0 0 5 0 7 8
6 9 0 5 0 3 0 0 4
5 0 0 0 6 0 0 3 0
0 0 8 0 1 0 6 5 0
Thx
At a glance, this is because you are running off the end of the array in various places.
for (x = 0; x <= MAX_POSSIBILITIES; x++) { // 0,1,2...8
See that <= you have there? That's your problem.
numbers is declared as bool numbers[MAX_POSSIBILITIES];, and array indices in C start at 0 and go to length - 1. Zero through seven in this case, but you are trying to access numbers[8].
You have the same issue elsewhere. a is declared as
int a[SUDOKU_X][SUDOKU_Y]; // int a[8][8];
and in getPossibleElements you are iterating from 0 to 8 inclusive, like so:
for (x = 0; x <= SUDOKU_X; x++) {
...
for (y = 0; y <= SUDOKU_Y; y++) {
...thus running off the end of your array again.
Same deal in printPossibilities.
Change MAX_POSSIBILITIES, SUDOKU_X and SUDOKU_Y to be 9 in your #defines and iterate from 0 to 8 by doing
for (x = 0; x < SUDOKU_X; x++) { // 0,1,2...8
One more thing. You should fix your fileName stuff too. Same deal. The last index in an array is length - 1, not length. If you had warnings cranked up when you compiled, it probably would have mentioned this one.
if (argc == 2) {
strncpy(fileName, argv[1], MAX_FILENAME - 2); // was MAX_FILENAME - 1
fileName[MAX_FILENAME - 1] = '\0'; // was [MAX_FILENAME]
}
I'm just getting started on a project and am having some trouble. The idea is to make an 8x8 array that randomly generates an array with ten blocks, one random starting point, and the top right corner being an 'exit'. Then the program will find the quickest path and the output will be the randomly generated array as well as the directions to the exit (i.e., Up, Up, Right, Right, Up, Left)
Obviously there are many steps that I have to take afterwards to get the array to do what I want, but I was wondering if anyone could help me figure out how to set one up so it looks something along the lines of (E=exit, X=block, Z=starting point)
0 0 0 0 X 0 0 E
0 0 X 0 0 0 0 0
0 0 0 0 0 0 X 0
0 X 0 0 X 0 0 0
0 0 0 0 0 0 0 X
X 0 0 X 0 Z 0 0
0 0 0 0 0 0 0 0
0 0 X 0 0 0 X 0
So far I have this program. It fortunately places the ten random -1s as well as the random starting point (I have it as '7' for now, but I will change it eventually), but I can't seem to guarantee that the upper right corner is not going to be one of the -1's that I need and then replaced by the 99 leaving the array with only nine -1's instead of the required ten.
#include <stdio.h>
#include <stdlib.h>
#define ROW 8
#define COLUMN 8
#define NUM 10
#define start 1
int main () {
int x, y;
int array[ROW][COLUMN];
for (x = 0; x < ROW; x++) {
for (y = 0; y < COLUMN; y++) {
array[x][y] = 0;
}
}
srand (time (NULL));
for (x = 0; x < NUM; x++) {
int t = rand () % ROW;
y = rand () % COLUMN;
if (array[t][y] != -1) {
array[t][y] = -1;
} else {
x--;
}
}
for (x = 0; x < start; x++)
{
int t = rand () % ROW;
y = rand () % COLUMN;
if (array[t][y] != 7) {
array[t][y] = 7;
} else { x--;
}
}
for (x = 0; x < ROW; x++) {
for (y = 0; y < COLUMN; y++) {
array[0][7] = 99;
printf ("%d ", array[x][y]);
}
printf ("\n");
}
return 0; }
any help would be greatly appreciated-I am a terrible programmer.
If I understand your question, then I would begin by initializing the array contents to zero (instead of -1) and I would also define the number of elements I want to randomly set to -1.
#include <stdio.h>
#include <stdlib.h>
#define ROW 8
#define COLUMN 8
#define NUM 10
int main (int argc, char *argv[]) {
int x, y;
int array[ROW][COLUMN];
for (x = 0; x < ROW; x++) {
for (y = 0; y < COLUMN; y++) {
array[x][y] = 0;
}
}
Then we can set NUM elements randomly to -1 like
srand (time (NULL));
for (x = 0; x < NUM; x++) {
int t = rand () % ROW;
y = rand () % COLUMN;
if (array[t][y] != -1) {
array[t][y] = -1;
} else {
x--;
}
}
Finally, we can print the results with something like
for (x = 0; x < ROW; x++) {
for (y = 0; y < COLUMN; y++) {
printf ("%d ", array[x][y]);
}
printf ("\n");
}
return 0;
I would have a 3-D array and have the first two dimensions randomized from 0-7 and the third dimension be randomized 0 and 1. 0 being a block and 1 being starting pointing. Having a for-loop for how many you want of each. Then fill the rest of the array with "0"'s