Please check the problem with this code.
It is showing compilation error when I'm running it on mycodeschool IDE.
problem statement is
Problem statement
Given a two dimensional array, print its mirror image if mirror is placed along one of the sides of the array.
Input
First line of input will contain a number T = number of test cases. Each test case will contain two positive integers n and m (1<=n, m<=50) on a single line separated by space. Next n lines will each contain a string of exactly m characters. Next line will contain a character 'V' or 'H'. If character is V, mirror is placed vertically along the right-most column. If the character is H, the mirror is placed horizontally along the bottom-most row.
Output
For each test case, print the n*m mirror image - n lines with strings of m character each. Print an extra empty line after output for each test case.
Sample Input
2
3 3
abc
def
ghi
V
3 4
1234
5678
9876
H
Sample Output
cba
fed
ihg
9876
5678
1234
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char a[10][10];
int i,j,t,m,n;
char s;
scanf("%d\n",&t);
for(j=0;j<t;j++)
{
scanf("%d%d\n",&m,&n);
for(i=0;i<m;i++)
{
scanf("%s",&a[i]);
}
scanf("\n%c",&s);
if(s=='V')//for image along rightmost vertical line
{
for(i=0;i<m;i++)
{
strrev(a[i]);
}
}
else if(s=='H')//for image along lowermost horizontal line
{
int t=0;
int b=m-1;
while(t<b)
{
char *temp = (char *)malloc((strlen(a[t]) + 1) * sizeof(char));//temporary variable to swap
strcpy(temp, a[t]);
strcpy(a[t], a[b]);
strcpy(a[b], temp);
free(temp);
t++;
b--;
}
}
for(i=0;i<m;i++)
{
printf("%s\n",a[i]);
}
printf("\n");}
return 0;
}
I think there is problem with taking input character array n line with exactly m characters...here is my code ........
int main()
{
int tt,i,j,r,c,t,ch;
char a[20][20];
scanf("%d",&t);
for(tt=1;tt<=t;t++)
{
scanf("%d\t%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%c",&a[i][j]);
}
printf("\n");
}
scanf("%c",&ch);
if(ch=='V')
{
for(i=0;i<r;i++)
{
for(j=(c-1);j>=0;j--)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else if(ch=='H')
{
for(i=(r-1);i>=0;i--)
{
for(j=0;j<c;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else
printf("error");
}
printf("");
getchar();
return 0;
}
Related
This code is intended to print a pattern with asterisk like this
If a number is entered such as 5
Then the program should print the following pattern. It should print * in order according to the number given and then decreasing
*****
****
***
**
*
But it is printing only one line. Please tell me what is the fault here.
#include<stdio.h>
int main()
{
int lines,lines2;
printf("Enter the number of lines : ");
scanf("%d",&lines);
lines2=lines;
for(;lines>0;lines--) {
for(;lines2>0;lines2--){
printf("*");
}
}
printf("\n");
return 0;
}
First: The following line should be placed between two loop:
lines2=lines;
Second: The following line should be placed before the { of outer loop:
printf("\n");
The Final solution is:
#include<stdio.h>
int main()
{
int lines,lines2;
printf("Enter the number of lines : ");
scanf("%d",&lines);
for(;lines>0;lines--)
{
lines2=lines;
for(;lines2>0;lines2--)
{
printf("*");
}
printf("\n");
}
return 0;
}
lines2=lines;
for(;lines>0;lines--)
{ for(;lines2>0;lines2--)
You only initialize lines2 once BEFORE the outer loop; that is why past the first line it is always zero. You should reset it per each line, assigning to current value of lines. This one is probably what you wanted to do:
for(; lines > 0; --lines) {
for(lines2 = lines; lines2 > 0; --lines2) {
See this!
It works!
#include<stdio.h>
main()
{ int lines,lines2;
printf("Enter the number of lines : ");
scanf("%d",&lines);
lines2=lines;
for(;lines>0;lines--)
{ for(lines2=lines;lines2>0;lines2--)
{ printf("*");
}
printf("\n");
}
}
You just changed some lines' places. You need to put newline into first loop:
int main() {
int lines,lines2;
printf("Enter the number of lines : ");
scanf("%d", &lines);
for(;lines>0;lines--) {
lines2=lines;
for(;lines2>0;lines2--){
printf("*");
}
printf("\n");
}
return 0;
}
I've tried to write a program that removes the duplicate values from an array. I've partly managed to do so since my program is able to remove any ONE of the numbers which are repeated TWICE in the array. So the problem is that if a number is repeated thrice only one of the number is removed, i.e. the other two is still left in the array, also if more than one number is repeated even then only the number which comes first in the array is removed. I really cannot understand what's wrong with my code and why is it unable to remove numbers that are repeated more than two times. I've already surfed through the internet regarding this issue and though I got different ways to remove the duplicate elements, I still don't know what's wrong with my code.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20];
printf("Enter n value \n");
scanf("%d",&n);
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++)
a[i]=a[i+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
24
It clearly fails to remove the other repeated element "24". Also if a number was repeated thrice only one of the number would be removed.
for(i=0;i<n;++i) // <-------------------------------------- for i
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++) // <--------------------------- for i
a[i]=a[i+1];
n-=1;
}
}
You are using the same loop variable for two loops, one nested inside the other. This cannot work. Use different variables. Live demo.
The Problem seem to lie in the if condition in second loop.
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
Simply put this piece of code after your if condition
if(a[i]==a[j])
and it will work.
My mistake, at first glence I thought you had problem with n after running this it worked.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20], count;
printf("Enter n value \n");
scanf("%d",&n);
count = n;
int j;
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(j=index;j<n;j++)
a[j]=a[j+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
You should name your iterator variables better so you might not confuse them in nested loops, or as you do, use the same twice in a nested loop.
This skips all variables after your first removal.
and you don't have to do this
if(pos==-1)
return pos;
skip the if as it is not necessary and if at this position posis not -1then you would have no return which would be UB I think.
I am trying to learn C and here i got a program in which we have to take the input from the user as n number os strings, compare it and arrange it in a alphabetical order. After arranging them in a alphabetical order , i have to only print the last name which was occurring in the order.
Here is the code for the above problem:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,len;
char a[50][50],temp[100];
char last ;
printf("Enter the number of elements you wish to order : ");
scanf("%d",&m);
printf("\nEnter the names :\n");
for (i=0;i<m;i++){
scanf("%s",a[i]);
}
for (i=0;i<m;i++){
for (j=i+1;j<m+1;j++) {
if (strcmp(a[i],a[j])>0) {
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
printf("\n\nSorted strings are : ");
for (i=0;i<m+1;i++){
printf("%s \n",a[i]);
}
return 0;
}
~
The Answer goes this way:
Enter the number of elements you wish to order : 4
Enter the names :
territory
states
hello
like
Sorted strings are : S$???
hello
like
states
territory
My question goes that why am i getting "S$???" and i want only the last word "territory should be printed out not all the names".
Can anyone let me know where am i going wrong? It will be a great help.
Thanks
Tanya
You are sorting m+1 strings with indexes [0..m]. But you input only m strings.
Your indexes should never go above m-1.
\Check this code
In your code you get input as 4
then
a[0]=territory
a[1]=states
a[2]=hello
a[3]=like
But your code runs upper loop at most 3 time then i=3
In next loop j=i+1 then j=4
but a[4]=not exists
Thats why "S$???" occurs
Solution:
#include<stdio.h>
#include<string.h>
int main() {
int i, j, m, n, len;
char a[50][50], temp[100];
char last;
printf("Enter the number of elements you wish to order : ");
scanf("%d", &m);
printf("\nEnter the names :\n");
for (i = 0; i < m; i++) {
scanf("%s", a[i]);
}
for (i = 0; i < m - 1; i++) { // m - 1 enough maximum i = 2;
for (j = i + 1; j < m; j++) { // j maximum j = i + 1 j = 3
if (strcmp(a[i], a[j]) > 0) {
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);
}
}
}
printf("\n\nSorted strings are : "); // print all words in sorted order
for (i = 0; i < m; i++) {
printf("%s \n", a[i]);
}
printf("Last Word:\n");
printf("%s\n", a[m - 1]); // a[3] contains last name
return 0;
}
this was my program it worked for me in turbo c++
#include<conio.h>
#include<iosream.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
char name[100];
int c=0;
// to take the name including spaces
gets(name);
//this line is to print the first character of the name
cout<<name[0];
//this loop is to print the fist character which is there after every space
for(int l=0;name[l]!='\0';l++)
{
if(name[l]==' ')
{
cout<<"."<<name[l+1];
//l+1 is used because l is the space and l+1 is the character that we need
}
}
//this loop is to find out the last space in the entire sting
for(int i=0;name[i]!='\0';i++)
{
if(name[l+1]==NULL)
{
//here we fond the last space in the string
for(int j=i;name[j]!=' ';j--)
{
c=j+1;
}
// c=j+1 is used bacause we have already taken the first letter of the that word
//no need of taking it again
}
}
//this loop starts from the last space and the position(of space) is stored in k
for(int k=c;name[k]!='\0';k++)
{
cout<<name[k];
}
getch();
}
output:
anentt ranjan shukla
a.r.shukla
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.
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.