Printing Number Pattern in C - c

input: 'n' which will always be an odd number
output for n=3
1
2 4
3
output for n=5
1
2 4
3 5 7
6 8
9
Help me in understanding the logic.
What I could understood was that in each line the numbers are having a difference of 2.

If your task is to write a program to print those patterns, I suggest you start smaller. First write a program to print this pattern:
0 1 2
The outline of your program will look something like this:
#include <stdio.h>
int main()
{
int n = 3;
int i;
for(i = 0; i < 3; i++) {
// you fill in something here
}
}
That should be easy. Once you get that to work, make a simple modification so it prints this:
1 2 3
That should be easy. Once you get that to work, try writing a program to generate this pattern:
1
1 2
1 2 3
That should be pretty easy. Once you get that to work, try modifying it so it generates this pattern:
1
1 2
1 2 3
And once you get that to work, you can try printing the diamond patterns in your assignment.

Related

Segmentation fault accessing 2D array

I have a 2D array representing a sudoku grid defined like this:
int** sudoku = (int**)malloc(sizeof(int*) * 9);
for(i = 0; i < 9; i++)
sudoku[i] = (int*)malloc(sizeof(int) * 9);
I've run it through a function that iterates through every element and prints, which works fine (displays 81 zeros). But then, I hand it to another function that reads a grid from a file into this array. Here's what it looks like (with a bunch of printf statements I'm using for debugging omitted).
void readSudokuFile(char* filename, int*** grid) {
FILE* file;
int i, j, curr;
file = fopen(filename, "r");
for(i = 0; i < 9; i++) {
for(j = 0; j < 9; j++) {
fscanf(file, "%d", &curr);
*grid[i][j] = curr;
}
}
fclose(file);
}
When the function is running, it appears to read find for the first row, but when it gets to the second row and tries to insert a value into sudoku[1][0], I get a seg fault. This is what the output looks like with my printfs in:
Reading line 0...
Reading col 0... got 6
Reading col 1... got 2
Reading col 2... got 4
Reading col 3... got 5
Reading col 4... got 3
Reading col 5... got 9
Reading col 6... got 1
Reading col 7... got 8
Reading col 8... got 7
Reading line 1...
Reading col 0... got 5
Segmentation fault(core dumped)
This is the file I'm trying to read in:
6 2 4 5 3 9 1 8 7
5 1 9 7 2 8 6 3 4
8 3 7 6 1 4 2 9 5
1 4 3 8 6 5 7 2 9
9 5 8 2 4 7 3 6 1
7 6 2 3 9 1 4 5 8
3 7 1 9 5 6 8 4 2
4 9 6 1 8 2 5 7 3
2 8 5 4 7 3 9 1 6
I'm compiling using gcc with -Wall and -pedantic, and am getting no compiler warnings.
I've googled around for a few hours to no avail, I'm not exactly sure what's going wrong here. Any help would be greatly appreciated.
To avoid pointer bugs you just ran into I suggest to simplify your dynamic 2D-array allocation like this ->
int (*array) [Y] = malloc(sizeof(int[X][Y]));
Access your array like this ->
int g=array[0][0];
And set like this ->
array[0][0]=0;
That will simplify your solution a lot and you also get a continuous memory block containing your 2D-array as a bonus. That will also simplify writing and reading files as you do not need to traverse each element if you do not necessarily have to do that for other reasons.
/A
Try modify your function like this:
void readSudokuFile(char* filename, int** grid) {
// ...
grid[i][j] = curr;
}
and invoke it like this:
readSudokuFile(filename, sudoku)

Not getting desired output. Is there something inherently wrong in the logic?

the input depicts no of tests followed by size of input array followed by the array and the output either gives -1 or the square of the largest prime in the given array.
I am providing the code and the expected and actual output along with sample standard input used.
standard input:
3
5
1 4 6 8 10
3
2 2 9
2
156 13
expected output | getting
-1 -1
4 4
169 -1
#include <stdio.h>
int main(){
int test,size;
int i,j;
scanf("%d\n",&test);
while(test>=1){
scanf("%d\n",&size);
int data[size],factors=0,max=0;
for(i=0;i<size;i++){
scanf("%d ",&data[i]);
for(j=1;j<=data[i];j++){
if(data[i]%j==0){
factors+=1;
}
}
if((factors==2) && (data[i]>max)){
max=data[i];
}
}
if(max>=2){
printf("%d\n",max*max);
}else{
printf("%d\n",-1);
}
max=0;
test-=1;
}
}
ok debugged and got the answer. Factors needs to be initialized inside the next for loop.

Wrong Output,but why?

User will give N,S respectively
If the value of N is 3, i.e i have 3 boxes and the input of S is 6.
Then I am coding in such a manner that in the 1st case(n=3), 1 is added to each box,then again(n=2) 1 is added to each box (A,B,C) excluding the last box, again(n=1) 1 is added to each box excluding the last box as well as the second last box.
And the total number of balls left in each box is to displayed after distribution of total value S,
For example :
It looks like this, where a,b,c are the 3 boxes.
n=3 and s=6
A B C
1 1 1
1 1
1
-----
3 2 1 //output to be displayed
again if the input of n ans s are 4 and 9
then output would be,
A B C D
1 1 1 1
1 1 1
1 1
-------
3 3 2 1 //output to be displayed
again if the input of n ans s are 3 and 4
then output would be,
A B C D
1 1 1 1
1
-------
2 1 1 1 //output to be displayed
again if the input of n ans s are 4 and 2
then output would be,
A B C D
1 1
-------
1 1 0 0 //output to be displayed
for n max value of s = n*(n+1)/2
Actually in this case the the complexity of the code cant be O(n^2). SO this was my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,s,y,x1;
scanf("%d%d",&n,&s);
int b[n];
memset(b,0,n);
x1=n;
j=0;
while(s>0)
{
y=j%n;
if(j!=n-1)
{
b[y]++;
s--;
j++;
}
else
{
b[y]++;
s--;
j=0;
n--;
}
}
for(i=0;i<x1;++i)
{
printf("%d ",b[i]);
}
return 0;
}
But after executing the code,The output given by my code:
where n = 4 and s = 9
the output is : 3 32631 -747449654 32629
where n = 3 and s = 5
the output is :2 2 4195586
Why is this happening ? I dont want to use nested for loops!
But whats wrong in here ? please help me!
void * memset ( void * ptr, int value, size_t num );
num is the number of bytes, so
memset(b,0,n);
should be
memset(b,0,n*sizeof(int));
Live Demo
The third argument to memset is the number of bytes to set. Since b is an array of int, it has 4*n bytes that need to be cleared, assuming 32-bit ints. The correct code is
memset( b, 0, sizeof(b) );

I get an Unexpected Output

In my book , this code is given.They say that the output is 2 2 2 2 2 2 3 4 6 5
Please explain is this correct or not ? If not then what is the correct o/p?
#include <stdio.h>
#include <string.h>
main()
{
int c[]={2,8,3,4,4,6,7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++){
printf(" %d",*c);
++q;
}
for(j=0;j<5;j++){
printf(" %d",*p);
++p;
}
}
In first for-loop you are printing *c instead of *q:
printf(" %d",*c); // outputs `2 2 2 2 2` as first element, five times
should be:
printf(" %d",*q);
as I notice you increments q
output is 2 2 2 2 2 in first loop because of *c, c decays into address of fist element in this expression.
Edit
According to your code output should be as suggested by #ChronoTrigge (I notice latter):
First loop outputs five times 2 as I explained above
second loop will output first five elements in array a[] so output should be: 2 8 3 4 4
complete output: 2 2 2 2 2 2 8 3 4 4

How could I circularly shift each row of an 2-d array by two positions?

First of all I want to show you what actually I want........
if input is ...
2 3 4
5 6 6
7 5 4
the output should be ...
7 5 4
2 3 4
5 6 6 /*Each row is shifted circularly left by two positons */
I tried this code acc. to my knowledge (I am a beginner in C) and have written this thing ..
/*To shift row of a 4 * 5 matrix by 2 positons left*/
#include<stdio.h>
int main() {
int a[4][5],i,j,k,(*temp)[5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",*(a+i)+j);
}
for (k=1;k<=2;k++) {
for(i=0;i<=3;i++) {
temp = (a+i); /*I thought that *(a+i) will point to the address of each row and so I should take it in a variable which is capable of pointing to a row of 5 variables that why TEMP */
(a+i) = (a+i+1);
(a+i+1) = temp;
}
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(a+i)+j));
printf("\n");
}
return 0;
}
where am I wrong.....Please correct me ????
Your sample output looking like shifted along column :)
scanf("%d",*(a+i)+j); is not a good way, use
scanf("%d",&a[i][j]); instead
You tried to copy a row at temp = *(a+i);, but you can only
copy adresses here. temp is going to point a[i], but won't copy
it's data.
This code below gives
input
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
output
3 3 3 3 3
4 4 4 4 4
1 1 1 1 1
2 2 2 2 2
I have shifted columns like your sample and used a new array b instead of temp
#include<stdio.h>
int main() {
int a[4][5],i,j,b[4][5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",(*(a+i)+j));
}
for(i=0;i<=3;i++)
for(j=0;j<=4;j++)
{
*(*(b+i)+j)=*(*(a+((i-2+4)%4))+j);
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(b+i)+j));
printf("\n");
}
return 0;
}
Although the concept that you are driving at could be made to work (but there is a LOT wrong with it at the moment) using pointer arithmetic in this context makes the code look very complicated so I wonder why you don't try to rewrite this using array syntax.
For example you could write your output like this:
for(i=0;i<=3;i++)
{
for(j=0;j<=4;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
I think this is the easier syntax for a beginner to understand. Similarly the row cycle / swap is far more transparent in this form.

Resources