i have some trouble in typecasting - c

i am working in a university project that i should write a minesweaper game with some array ...
i have written my program
the program will print the minesweaper table some cell in the table has bomb the program should print * in that cell.
and other cell should print number of bomb in eight surrounding cell
I don't know how to print this double sub-scripted array that can print character and number both.
#include <stdio.h>
#define max 100
int main()
{
int a,row,column,n,x,y,counter,i,j;
char table[max][max]={0};
scanf("%d%d",&row,&column);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
table[x-1][y-1]='*';
}
for(counter=0,i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(table[i-1][j-1]=='*')
counter++;
if(table[i-1][j]=='*')
counter++;
if(table[i-1][j+1]=='*')
counter++;
if(table[i][j-1]=='*')
counter++;
if(table[i][j+1]=='*')
counter++;
if(table[i+1][j-1]=='*')
counter++;
if(table[i+1][j]=='*')
counter++;
if(table[i+1][j+1]=='*')
counter++;
if(table[i][j]!='*')
table[i][j]=counter;
counter=0;
}
}
for(counter=0,i=0;i<row;i++)
{
for(j=0;j<column;j++)
printf("%d ",(char)table[i][j]);
printf("\n");
}
}

Not an answer, but your code will not work because
if(table[i][j]!='*')
table[i][j]=counter;
^^^^^^^^^^^^^^^
Will overwrite what is in the location at i,j and the later passes will not see a '*' there. So basically your counts are going to be all messed up. You need one array for the '*' and another for the neighbor counts.

Related

sorting an array related to a string array in c

I'm trying to sort a string related to an array based on the values of the array, for some reason the sorting part inst working.. when i try to sort the elements related to each other and print them for some reason the print comes out incredibly randomly
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sorting(char name[][10],double average[],int size);
int main()
{
double sales1[10],sales2[10],sales3[10],average[10],test,totalm=0,totalfm=0,test2;
int i=0,j;
char name[10][10],gender[10];
printf("Please input the name, the gender, and the sales for the\nfirst three months followed by a spacebar after each element\n");
scanf("%s %c %lf %lf %lf",&name[0],&gender[0],&sales1[0],&sales2[0],&sales3[0]);
average[i]=(sales1[i]+sales2[i]+sales3[i])/3;
while(strcmp(name[i],"enough")!=0)
{
i++;
printf("Please input the name, the gender, and the sales for the\nfirst three months followed by a spacebar after each element\n");
scanf("%s %c %lf %lf %lf",&name[i],&gender[i],&sales1[i],&sales2[i],&sales3[i]);
average[i]=(sales1[i]+sales2[i]+sales3[i])/3;
}
sorting(name,average,i);
j=i;
while(i>=0)
{
if(gender[i]=='m')
totalm=totalm+average[i];
else
totalfm=totalfm+average[i];
i--;
}
while(j>=0)
{
test2=strcmp(name[j],"enough");
if(test2!=0)
printf("%s\t%f\n",name[j],average[j]);
j--;
}
printf("total male sales are %f\n",totalm);
printf("total female sales are %f\n",totalfm);
}
int sorting(char name[][10],double average[], int size)
{
int i=0;
double temp;
char ntemp[20][20];
while(i<=size)
{
if(average[i+1]>average[i])
{
temp=average[i];
strcpy(ntemp[i],name[i]);
average[i]=average[i+1];
strcpy(name[i],name[i+1]);
average[i+1]=temp;
strcpy(name[i+1],ntemp[i]);
}
i++;
}
}
thanks!
i think the sorting you have applied seems to be wrong on very first condition of your sorting function
while(i<=size)
{
if(average[i+1]>average[i])
{
temp=average[i];
assume the condition when i is equals to size then the average[i+1] will be point to nothing on you can say zero that value, which you don't set.
so try to correct this code
for(i=0;i<n;i++)
{
for(k=0;k<n-i-1;k++)
{
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
}
this is the bubble sort where you always iterate one less than the last pass.
for more see here
Thisn't a valid sort. You bubble one element to its proper position. Lookup bubble sort: it has two loops. The inner loop moves the current element, the outer loop loops over all remaining, unsorted elements.
See also the other comments to your posting for more bugs.

Removing repetitions of same numbers in an array

Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++

how to find array element from user input

friend i am new in c ,so i face problem in a ,code, plz if there is any wrong in my logic,take it as a pardon eye,
I am trying to find the element in a two dimensional array, so i have declare a two dimensional array in my code, i will take a user input, the input will compare with data in an full array a column index of two dimensional array, if any data found similar of that column index then it will give the same row data of another column of array. if i give a input in my code it is giving output of the number is not in array index, though the number is in the array index, so i dont understand where is my fault.
plz help me to fix the problem.
here is my code :
#include<stdio.h>
int main()
{
int arr[10][3]={{1,5},
{2,8},
{3,27},
{ 4,64},
{5,125},
{6,216},
{ 7,343},
{8,512},
{ 9,729},
{ 10,1000}};
int i, num;
printf("Enter a number\n");
scanf("%d",&num);
for(i=0;i<10;i++)
{
if (num==arr[i][0])
printf("%d",arr[i][1]);
break;
}
if (num==10)
printf("the number is not there");
return 0;
}
You have an errant semi-colon:
if (num==10);
printf("the number is not there");
That call to printf will run each time because there is no body for the if statement. With better formatting:
if (num==10);
printf("the number is not there");
As #zoska points out, you also have the same bug here:
if (num==arr[i][0]);
I would do the following three changes at the minimum:
Change int arr[10][3] to int arr[10][2]
Change
if (num==arr[i][0]);
printf("%d",arr[i][1]);
to
if (num == arr[i][0]) {
printf("%d",arr[i][1]);
}
Change
if (num==10);
printf("the number is not there");
to
if (i == 10) { // note: 'num' changed to 'i'
printf("the number is not there");
}
Your code should look like this in the future
#include <stdio.h>
int main(void)
{
int arr[10][2] = {
{1,5},
{2,8},
{3,27},
{4,64},
{5,125},
{6,216},
{7,343},
{8,512},
{9,729},
{10,1000}
};
int i, num;
printf("Enter a number\n");
scanf("%d", &num);
for(i = 0; i < 10; i++)
{
if (num==arr[i][0]) {
printf("%d", arr[i][1]);
break;
}
}
if (i == 10) {
printf("the number is not there");
}
return 0;
}

A fractal in c - Sierpinsky triangle

I am trying to code a project in c, that displays a fractal called Sierpinski fractal, (where the nodes are represented by '#'). So a 1-sierpinski triangle looks like :
##
#
a 2-sierpinski triangle
####
# #
##
#
and so on... Here's a link to find what it looks like : http://fr.wikipedia.org/wiki/Triangle_de_SierpiƄski
I was told it could be done without any loop, just by recursive method. So I tried something like :
//extracting the power of two's index
int puiss_2(int N){
int i=0,j=1;
for(i=0;i<N;i++){
j=j*2;
i++;
}
return j;
}
//the recursive method
void fractal(int N)
{
int M;
M= puiss_2(N);
if(M==0){
printf("##\n");
printf("# ");
}
else{
fractal(N-1);
fractal(N-1);
printf("\n");
fractal(N-1);
printf(" ");
}
}
int main()
{
int N;
scanf("%d",&N);
fractal(N);
}
Of course it didn't work because, when I jump to a line, I can't reverse it. So when I call it two times :
fractal(N-1);
fractal(N-1);
two contiguous motives are not gathered one aside the other... Does anyone has an idea on how to
make that ? Or perhaps I went completely wrong in my algo's design?
Here's some code that is perhaps complicated but recursive !
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sierpinsky(int N, char c[1000]){
int i=0,j,k,l,born;
for(i=0;i<N;i++){printf("%c",c[i]);}
printf("\n");
if(N==1){}
else{
if((c[0]=='#')&&(c[1]=='#')&&(c[2]=='#')){
for (j=0;2*j<N;j++){
if(c[2*j]=='#'){
c[2*j]='#';c[2*j+1]=' ';
}
else{
c[2*j]=' ';c[2*j+1]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2]=='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]='#';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2] !='#')){
k=0;
while(c[k+1] !='#'){k++;}
born = k+1;
j=0;
while(j<N){
if((c[j]=='#')&&(c[j+born]=='#')){
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else if ((c[j]!='#')&&(c[j-1+born]=='#')&&(c[j-1+2*born] !='#'))
{
c[j-1]='#';
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else{
c[j-1]= ' ';
c[j]=' ';
j++;
}
}
}
else if ((c[0] =='#')&&(c[1] =='#')&&(c[2] !='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else{}
sierpinsky(N-1, c);
}
}
int main()
{ int i,size;
scanf("%d",&size);
char c[1000];
for(i=0;i<size;i++){c[i]='#';}
for(i=size;i<1000;i++){c[i]='a';}
sierpinsky(size, c);
}
I think you dont need recursion for this. Let a triplet of # be 1 set. So The value of n = No. of levels of the set you're supposed to print one below the other. In the first line, print the set n times. In the next line, n-1 times, and so on. Try it iteratively.
Edit : If you were looking for a recursive solution, kindly ignore my answer.
you can probably code this using the pascal triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
If you can print out this triangle as a whole with a loop, then , perhaps you can skip over even numbers.
to print the plain triangle just count the number of spaces in relation to the number of lines you want and code using a for loop(or a couple of them).Check which (pascal) number corresponds to which printing and skip over the even ones.

Sorting letters C programming

I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.

Resources