The question is:
Given a 2-D array A of size M x N . Write a program in C to print A in spiral form starting from bottom right corner in anti-clockwise direction. There must be space after each element. Note the following
1<=M
N<=20
0<=Aij<=1000
Example:
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1
Input
3 3
1 2 3
4 5 6
7 8 9
Output
9 6 3 2 1 4 7 8 5
I have tried it....can anyone help me with this.
#include<stdio.h>
int main()
{
int m,n;
scanf("%d%d",&m,&n) ;
int matrix[m][n] ;
int last_row = (m-1) ;
int last_column = (n-1) ;
int first_row = 0;
int first_column = 0;
int i=0,j=0,r=0,c=0;
while(i<m)
{
while(j<n)
{
scanf("%d",&matrix[i][j]) ;
++j;
} j=0;
++i;
}
while(first_column <=last_column && first_row <= last_row)
{ for( r=last_row ; r>=first_row ; --r)
{ c=last_column;
printf("%d ",matrix[r][c]) ;
} --last_column;
for (c=last_column; c>=first_column ; --c)
{
r=first_row;
printf("%d ",matrix[r][c]) ;
} ++first_row ;
for(r=first_row; r<=last_row; r++)
{
c=first_column;
printf("%d ",matrix[r][c]) ;
} ++first_column;
for(c=first_column ; c<=last_column ; c++)
{
r=last_row;
printf("%d ",matrix[r][c]) ;
} --last_row;
}
return 0;
}
The current output which I am getting is :
Input
8 1
1
2
3
4
5
6
7
8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7
Related
I tried editing the loop for counter so that the last line of printf is non negative but I'm not too sure which part of the loop to edit.
#include <stdio.h>
void printPatternHere(int height);
int main() {
int height;
printf("Enter the number of rows: ");
scanf("%d", &height);
printf("printPattern: \n");
printPatternHere(height);
return 0;
}
void printPatternHere(int height) {
int n, c, row, t = 1;
int counter = 0;
for (row = 1; row <= height; row++) {
for (c = 1; c <= height - row; c++)
t = row; // start each line with row number
for (c = 1; c <= row; c++) {
counter++;
if (counter > 9) {
t = t % 10;
}
printf("%d ", t);
t++;
}
for (c = 1 ; c < row; c++) {
t--; //reset row number
}
printf("\n");
}
}
If userinput is 14, its output should be something like this:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 0 1
7 8 9 0 1 2 3
8 9 0 1 2 3 4 5
9 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 0 1
2 3 4 5 6 7 8 9 0 1 2 3
3 4 5 6 7 8 9 0 1 2 3 4 5
4 5 6 7 8 9 0 1 2 3 4 5 6 7
Your function is cluttered too much, for loops are more than enough. Code below will be enough. Better to debug to grasp the idea.
// in "main"
printPattern3(height); // wrong function name!
printPatternHere(height); // corrected.
void printPatternHere(int height) {
int c, row;
for (row = 1; row <= height; row++) {
for (c = 0; c < row; c++) {
printf("%d ", (c + row) % 10);
}
printf("\n");
}
}
Your code is too complicated. The output of the pyramid can be done simpler.
For example this loop
for (c = 1; c <= height - row; c++)
t = row; // start each line with row number
does not make a sense.
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%u ", ( j + i + 1 ) % Base );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 14
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 0 1
7 8 9 0 1 2 3
8 9 0 1 2 3 4 5
9 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 0 1
2 3 4 5 6 7 8 9 0 1 2 3
3 4 5 6 7 8 9 0 1 2 3 4 5
4 5 6 7 8 9 0 1 2 3 4 5 6 7
Enter a non-negative number (0 - exit): 0
If you want to write a separate function that will output the pattern then the program can look the following way.
#include <stdio.h>
FILE * display_pyramid( unsigned int n, FILE *fp )
{
const unsigned int Base = 10;
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
fprintf( fp, "%u ", ( j + i + 1 ) % Base );
}
fputc( '\n', fp );
}
return fp;
}
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
fputc( '\n', display_pyramid( n, stdout ) );
}
return 0;
}
I am trying to get my program to count down after counting up to ten. I have tried to alter the code from counting up to make it count down to no avail.
#include <stdio.h>
void count(int k)
{
if (k > 0) {
count(-k + 1);
printf("%d", k);
}
else {
if (k == 0)
{
printf("%d,", k);
}
else {
count(k + 1);
printf("%d,", -k);
}
}
}
int main(int argc, char ** argv)
{
count(10);
getchar();
return 0;
}
Here is a simple example of the recursion which does this, illustrating Eugene's comment:
#include <stdio.h>
void count(int n) {
if (n > 10) {
printf("\n");
return;
}
printf("%d ", n);
count(n+1);
printf("%d ", n);
}
int main() {
count(0);
printf("\n");
return 0;
}
it counts up on the way into recursion and counts down while it exits it. Actually on the way down it only re-prints the state which it was before diving into the next level:
0 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1 0
The function can be easy implemented if to use a static local variable inside it. For example.
#include <stdio.h>
void count(unsigned int n)
{
static unsigned int m;
printf("%u ", m);
if (n != m)
{
++m;
count(n);
--m;
printf("%u ", m);
}
}
int main( void )
{
const unsigned int N = 10;
unsigned int i = 0;
do
{
count(i);
putchar('\n');
} while (i++ != N);
return 0;
}
The program output is
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0
Within the function the static variable m behaves as an index in a for loop (or there will be more suitable a do-while loop).
At first it is initialized implicitly by zero (as any static variable)
static unsigned int m;
You can use the initializer explicitly if you want
static unsigned int m = 0;
then it is changed from 0 to n and afterward backward from n again to 0.
++m; // changing from 0 to n
count(n);
--m; // changing from n to 0
I'm trying to do an inverted triangle with numbers using C. I think the number part of the code is right, but the spacing isn't working.
#include <stdio.h>
//Declare function
int triangle(int n);
//Main
int main(void){
int height;
do{
printf("height:");
scanf("%d", &height);
}while (height < 1 && height > 9);
triangle(height);}
//Function
int triangle(int n)
{
int x,j,linhas, spaces;
//Number of lines
for(linhas = 0; linhas < n; linhas++){
//Print spaces NOT FUC****** WORKING!!!
for(spaces =0; spaces < (linhas + 1); spaces ++){
printf(" ");}
//Fill in the numbers
do{
//Increasing part till n
for (x = 0; x < n; x++){
printf(" %d ", (x+1));}
//Decreasin part from n
for (j = 0; j < (n-1) ; j++){
printf(" %d ", ((n-1) -j));}
//New line after each line
printf("\n");
n--;
}while(n > 0);}}
the output is :
height:5
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Anybody can help on the spacing the output should be:
height:5
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
So, the problem are in do loop. You decrease the count (variable n), stay in loop until n>0 and the 'spaces for' do one time. Try doing like this.
int triangle(int n)
{
int x,j,linhas, spaces, m;
//Number of lines
m=n;
for(linhas = 0; linhas < n; linhas++){
for(spaces =0; spaces < linhas; spaces ++){
printf(" ");
}
//do{
for (x = 0; x < m; x++){
printf(" %d ", (x+1));
}
for (j = 0; j < (m-1) ; j++){
printf(" %d ", ((m-1) -j));
}
printf("\n");
m--;
//}while(n > 0);
}
}
Your code is messed up. It should have 2 nested loops, like this:
while (n > 0)
{
for (...) // print decreasing
{
}
for (...) // print increasing
{
}
}
Then you stuff your spaces inside the 1st nesting layer:
while (n > 0)
{
for (...) // print spaces
{
}
for (...) // print decreasing
{
}
for (...) // print increasing
{
}
}
However, you have 3 nested layers!
for (linhas ...) // useless code - you should remove it!
{
for (...) // this code is misplaced
{
}
while (n > 0)
{
for (...) // print decreasing
{
}
for (...) // print increasing
{
}
}
}
The outer loop does just on iteration (because on the second iteration n is 0, and it exits). This code is unneeded and confusing - remove it, and then you will see where to place your code that prints spaces.
My five cents.:)
You can output the first digit separatly using symbol * as the field width in the format specifier and an appropriate number.
For example
printf( "%*u", 2 * i + 1, j );
^^^
Here is a demonstrative program
#include <stdio.h>
int main( void )
{
while ( 1 )
{
printf( "Enter the height of the pyramid (0- exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
printf( "\n" );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int j = 1;
printf( "%*u", 2 * i + 1, j );
while ( !( n - i < j + 1 ) ) printf( " %u", ++j );
while ( --j != 0 ) printf( " %u", j );
printf( "\n" );
}
}
return 0;
}
If to enter sequentially
9 8 7 6 5 4 3 2 1 0
then the program output is
Enter the height of the pyramid (0- exit): 9
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 8
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 7
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 6
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 5
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 4
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 3
1 2 3 2 1
1 2 1
1
Enter the height of the pyramid (0- exit): 2
1 2 1
1
Enter the height of the pyramid (0- exit): 1
1
Enter the height of the pyramid (0- exit): 0
you should try (keeping most of your code)
//Function
int triangle (int n)
{
int x, j, linhas, spaces;
int orig_n = n;
//Number of lines
for (linhas = 0; linhas < n; linhas++)
{
do
{
for (spaces = 0; spaces < (orig_n - n) * 3; spaces++)
{
printf (" ");
}
//Fill in the numbers
//Increasing part till n
for (x = 0; x < n; x++)
{
printf (" %d ", (x + 1));
}
//Decreasin part from n
for (j = 0; j < (n - 1); j++)
{
printf (" %d ", ((n - 1) - j));
}
//New line after each line
printf ("\n");
n--;
}
while (n > 0);
}
}
Hello everyone i am having a bit of trouble with a project for my class
We just started learning C and I can't get this assignment working, Its a bubble sorter.
I'm unfamiliar with the syntax as we just learned it, but i think the code itself, except the printValues, is working... not sure yet how to do that one
It has several errors come up but the first is
error: expected expression before 'if'
I am also not sure about the second for statement in `sort()
Intended Output:
Before:
[7 3 9 4 6 1 2 8 5 ]
[3 7 9 4 6 1 2 8 5 ]
[3 7 4 9 6 1 2 8 5 ]
[3 7 4 6 9 1 2 8 5 ]
[3 7 4 6 1 9 2 8 5 ]
[3 7 4 6 1 2 9 8 5 ]
[3 7 4 6 1 2 8 9 5 ]
[3 7 4 6 1 2 8 5 9 ]
[3 4 7 6 1 2 8 5 9 ]
[3 4 6 7 1 2 8 5 9 ]
[3 4 6 1 7 2 8 5 9 ]
[3 4 6 1 2 7 8 5 9 ]
[3 4 6 1 2 7 5 8 9 ]
[3 4 1 6 2 7 5 8 9 ]
[3 4 1 2 6 7 5 8 9 ]
[3 4 1 2 6 5 7 8 9 ]
[3 1 4 2 6 5 7 8 9 ]
[3 1 2 4 6 5 7 8 9 ]
[3 1 2 4 5 6 7 8 9 ]
[1 3 2 4 5 6 7 8 9 ]
[1 2 3 4 5 6 7 8 9 ]
After:
[1 2 3 4 5 6 7 8 9 ]
Here is my code so far:
#include <stdio.h>
#define MAX 9
//function prototypes
void printValues(){
printf("hi");
}//end printValues
void sort(){
int i;
int j;
for (i = 0; i < MAX-1; i++)
for (j = 0; j < MAX -1 - i)
if (*[i] > *[j + 1])
swap(*[j] *[j+1])
}//end sort
void swap(int*h, int*l){
int temp;
temp = *l
*l = *h
*h = temp
}//end swap
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
int main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
} // end main
I got it working.
Some errors in your code:
using function before declaring it
mixing & and *
missing semicolons
broken loop syntax (why did you put ; after it? That would be a do-nothing loop!)
I also added implementation of the printValues() function
Also, *[i] doesn't really mean anything. You already have array, so just values[i] will do it. You can use ampersand if you want to get it's address (make a pointer).
Also... using a global array for this is not a very good idea. It'd be much better if you passed the array to your sort function as an argument (pointer to it).
#include <stdio.h>
#define MAX 9
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
//function prototypes
void printValues(){
int i;
printf("[");
for(i = 0; i < MAX; i++) {
printf(" %d ", values[i]);
}
printf("]\n");
}//end printValues
void swap(int* h, int* l){
int temp;
temp = *l;
*l = *h;
*h = temp;
}//end swap
void sort() {
int i;
int j;
for (i = 0; i < MAX-1; i++) {
for (j = 0; j < MAX - 1 - i; j++) {
if (values[j] > values[j + 1])
swap(&values[j], &values[j+1]);
}
}
}//end sort
int main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
} // end main
Why am I getting a segmentation fault with this code?
/* solver.h header file */
10 struct options{
11 unsigned int one:1, two:1, three:1, four:1, five:1, six:1, seven:1, eight:1, nine:1;
12 };
13
14
15 /* structure to describe a cell */
16 struct cell{
17 short value;
18 struct options open_options;
19 };
solver.c:
5 #include <stdio.h>
6 #include "solver.h"
7
8
9
10
11
12 int main(){
13 struct cell board [9][9];
14 int i=0,j=0;
15
16
17 for(i = 1; i<10; i++)
18 for(j = 1; j<10; j++)
19 (board[i][j]).value = j;
20
21 for(i = 1; i<10; i++){
22 for(j = 1; j<10; j++)
23 printf(" %d",(board[i][j]).value);
24 printf("\n");
25 }
26 return 0;
27 }
output:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Segmentation fault: 11
Arrays are indexed from 0, so the loops should be for(i = 0; i<9; i++) and not for(i = 1; i<10; i++)
In your case, you probably override part of the stack, but in general, going out of boundaries results in undefined behavior.
some_type array[9]; defines array to be an array of 9 elements, with subscripts going from 0 to 8 inclusive. You can't use array[9].
board[9][9] will contain elements with indices in the range 0...8, not 1...9. When you assigned to board[9][whatever], you actually overwrote memory that didn't belong to you, and this happened to cause the program to explode when return 0 handed control back to the C runtime and it started walking its structures to perform shut down.