I am working on program that takes matrix from input file like this. where first row represents parameters of matrix - rows,cols,0 for changing odd cols and 1 for changing even cols
5 5 0
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
and now I have to take odd clomuns (example: 7 4 1) and print it backwards so it will look like this ( 1 4 7 ) if I had 3x3 matrix it would looks like:
1 2 3
4 5 6
7 8 9
output matrix with odd cols printed backwards
7 2 9
4 5 6
1 8 3
and this is code I have so far
int r = 0, c = 0, odds = 0,n = 0,i,j;
FILE *fp,*fp2;
fp = fopen(argv[1],"r");
fp2 = fopen(argv[2],"w");
if(!fp){
printf("file doesnt exist\n");
}
if(!fp2){
printf("file doesnt exist\n");
}
fscanf(fp,"%d %d %d", &r, &c, &odds);
n = r *c;
int* matrix= (int*)malloc(r*c* sizeof(int));
for(int i = 0; i < n; i++)
{
fscanf(fp,"%d",&matrix[i]);
}
if(odds == 0){
for(int i = 0; i < n; i++){
if(i%2==0){
matrix[i] = i;
}
}
}else if(odds == 1){
for(int i = 0; i < n; i++){
if(i%2!=0){
matrix[i] = ;
}
}
}
for(i = 0;i < n; i++)
{
if(i % s == 0 ){
fprintf(fp2,"\n");
}
fprintf(fp2,"%d ",matrix[i]);
}
fclose(fp);
fclose(fp2);
return 0;
}
and my problem is with backwarding the cols, which is supposed to happen here
if(odds == 0){
for(int i = 0; i < n; i++){
if(i%2==0){
matrix[i] = i;
}
}
}else if(odds == 1){
for(int i = 0; i < n; i++){
if(i%2!=0){
matrix[i] = ;
}
}
}
1st if is for printing even cols backwards and 2nd is for odd cols
and as you can see the matrix is in my program represented by normal array
which wasn't my idea, but teachers, so its supposed to work like this
1 2 3|4 5 6|7 8 9 ----> 7 2 9|4 5 6|1 8 3
ok I just found out about map indexing, so now each position in array is presented as matrix[j+(i*r)] so for example 1st position in the 3x3 matrix above would be something like this: matrix[1+(0*3)], 4th pos would be matrix[1+(1*3)] etc...
So now my question is how to index the opposite postion in the column.
code update:
for(int i = 0; i < r; i++){
for(int j = 1; j < c; j++){
if(i%2!=0){
matrix[j+i*r] = ....;
}
}
}
In both instances you want to replace matrix[i] with an element further up in the matrix.
n n
1 2 3
4 5 6
7 8 9
In this example for replacing odd values, 1 and 7 should be switched. The difference in the indices is 6. However this is dependent on the matrix dimensions. The general formula would be:
n^2 - n - r*(2n) + i
where r is the row you are on. Since you are flipping the matrix you only have to do rows up to (n-n%2)/2.
Here is some python code to clarify
for i in range(0,int((matrixdim-matrixdim%2)/2)*matrixdim):
if ((i-r)%2) == 0:
temp = matrix[i]
matrix[i] = matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i]
matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i] = temp
if ((i+1)%matrixdim) == 0:
r += 1
Using i,j notation
for i in range(0,int((matrixdim-matrixdim%2)/2)):
for j in range(0, matrixdim):
pos = j + matrixdim*i
temp = matrix[pos]
if j%2 == even:
matrix[pos] = matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos]
matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos] = temp
Related
#include<stdio.h>
int main()
{
int n ;
printf("Input the number of rows: ");
scanf("%d", &n);
for(int i = 1; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
if(j == i + 1)
{
break;
}
printf("%3d", j);
}
for(int j = i - 1 ; j > 0; j--)
{
if(j == 0)
{
break;
}
printf("%3d", j);
}
printf("\n");
}
}
Program session
Number of rows for this output n = 3
My output:
1
1 2 1
1 2 3 2 1
Preferred output:
1
1 2 1
1 2 3 2 1
This here is an exercise where I have to print a pyramid of numbers where the central number of the pyramid is the number of the row. I understand the logic but as you can see I have not been able to fulfill the task successfully. Any tips?
As pointed out by #WeatherWane, you need to add logic to add extra spaces. If you notice carefully, number of spaces on each line(excluding padding you add with %3d is equal to 3 * (n - i)). You can create a simple method like this to add spacing:
void addSpaces(int N, int currentIndex, int padding) {
for (int index = currentIndex; index < N; index++)
for (int spaces = 0; spaces < padding; spaces++)
printf(" ");
}
then you can call it from your first for loop like this:
for(int i = 1; i <= n ; i++)
{
addSpaces(n, i, 3);
for(int j = 1 ; j <= i ; j++)
{
// ...
I tested it and it seems to align it correctly:
c-posts : $ ./a.out
Input the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
I wrote a function to print the below pattern.
For example, if the n value is 4 the pattern is
1
2 7
3 6 8
4 5 9 10
Or if the value of n is 5, then the pattern is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
My function gives me the first two block but not the next block. I'm stuck here for long time!
My function is
int printPattern(int n) {
int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
for(row = 1; row <= n; row++) {
fwdCtr = row;
for(column = 1; column <= row; column++) {
if(column % 2 != 0) {
printf("%d ", fwdCtr++);
} else {
evenCtr = fwdCtr + ctr;
printf("%d ", evenCtr);
ctr = ctr - 2;
}
}
printf("\n");
}
}
What I get is
1
2 7
3 6 4
4 5 5 4
Please give suggestions of changes!
The following code should do it:
#include <stdio.h>
void f(int n)
{
for (int i = 0; i < n; ++i)
{
for (int j=0; j<=i; ++j)
{
// Calculate the numbers used so far by previous columns
int x = 0;
for(int v=0; v<j;++v)
{
x = x + (n-v);
}
if ((j % 2) == 0)
{
// even columns
printf("%d ", x+i-j+1);
}
else
{
// odd columns
printf("%d ", x+n-i);
}
}
printf("\n");
}
}
int main(void)
{
f(5);
return 0;
}
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
The easy thing to do is just print the right number based on the row and column and the value of n, like this
int main(void)
{
int n = 20;
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++)
printf("%3d ", 1 + col*n - (col-1)*col/2 + (col%2 ? n-1-row : row-col));
printf("\n");
}
}
I'm trying to create a 2d-array in bubble sort, arranged 25 numbers 5 by 5 in ascending order
my inputs
Enter 25 integers:
Input No.[0][0]: 4
Input No.[0][1]: 5
Input No.[0][2]: 8
Input No.[0][3]: 9
Input No.[0][4]: 4
Input No.[1][0]: 2
Input No.[1][1]: 1
Input No.[1][2]: 0
Input No.[1][3]: 2
Input No.[1][4]: 4
Input No.[2][0]: 6
Input No.[2][1]: 7
Input No.[2][2]: 4
Input No.[2][3]: 5
Input No.[2][4]: 5
Input No.[3][0]: 4
Input No.[3][1]: 8
Input No.[3][2]: 9
Input No.[3][3]: 1
Input No.[3][4]: 2
Input No.[4][0]: 4
Input No.[4][1]: 5
Input No.[4][2]: 2
Input No.[4][3]: 1
Input No.[4][4]: 9
my output shows
Ascending:
4 4 5 8 9
0 1 2 2 4
4 5 5 6 7
1 2 4 8 9
1 2 4 5 9
as you can see its not in proper arranged, it only arranged the 5 numbers each lines not the whole numbers
can anybody help arranged my integers like this
Ascending:
0 1 1 1 2
2 2 2 4 4
4 4 4 4 5
5 5 5 6 7
8 8 9 9 9
this is my code so far
int main(){
int rows = 5, cols = 5;
int arr[rows][cols];
int i,j,k,swap;
printf("Enter 25 integers:\n");
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
printf("Input No.[%d][%d]: ", i+0,j+0);
scanf("%d", &arr[i][j]);
}
}
for(k = 0; k < rows; k++){
for(i = 0 ; i < cols; i++){
for(j = i + 1; j < cols; j++){
if(arr[k][i] > arr[k][j]){
swap = arr[k][i];
arr[k][i] = arr[k][j];
arr[k][j] = swap;
}
}
}
}
printf("Ascending:\n");
for( i = 0 ; i < rows; i++){
for( j = 0 ; j < cols; j++){
printf("%3d", arr[i][j]);
}
printf("\n");
}
getch();
}
Improving on Ahmad's answer, I would like to add the following code (for shorting the table in ascending order):
#include <stdio.h>
#define COL 5
#define ROW 6
int main()
{
int temp, t, i, j;
int arr[ROW][COL]={30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
for(t=1; t<(ROW*COL); t++)
{
for(i=0; i<ROW; i++)
{
for(j=0; j<COL-1; j++)
{
if (arr[i][j]>arr[i][j+1])
{
temp=arr[i][j];
arr[i][j]=arr[i][j+1];
arr[i][j+1]=temp;
}
}
}
for(i=0; i<ROW-1; i++)
{
if (arr[i][COL-1]>arr[i+1][0])
{
temp=arr[i][COL-1];
arr[i][COL-1]=arr[i+1][0];
arr[i+1][0]=temp;
}
}
}
for(i=0; i<ROW; i++)
{
printf("\n");
for(j=0; j<COL; j++)
printf("%3d", arr[i][j]);
}
return 0;
}
replace the input with your table and the definitions with the size of your given array and you're done.
output of the above when executed:
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 28 29 30
void twoDimBubbleSort(int** arr, int row, int col) {
for (int i = 0; i < (row * col); ++i) {
for (int j = 0; j < (row * col) - 1; ++j) {
int cr = j / col; // current row
int cc = j % col; // current column
int nr = (j + 1) / col; // next item row
int nc = (j + 1) % col; // next item column
if (arr[cr][cc] > arr[nr][nc])
swap(&arr[cr][cc], &arr[nr][nc]); // any way you want to swap variables
}
}
}
You don't necessarily need to create a 1D array, you can consider your 2D array is a 1D array and transform coordinates when you set/get them.
Consider a structure point with x and y, and ARR_LEN is 5.
int from2Dto1D(point p){ return p.x+ p.y*ARR_LEN;}
Point from1Dto2D(int i){ Point p; p.x = i%ARR_LEN; p.y=i/ARR_LEN; return p;}
Now you can use the normal bubble sorting algorithm with a 1D index on 2D squares array, you just need to convert your index into 2 Point and access/switch data using these Point. (2 because you need a Point with index and a Point with index+1
Put all the array elements from 2-D array to 1-D array then
sort that 1-D array and then put 1-D array in the matrix format
Try this code ....works according to the above given logic
#include<stdio.h>
int main(){
int arr[5][5],l=0;
int result[25],k=0,i,j,temp;
arr[0][0]= 4;
arr[0][1]= 5;
arr[0][2]= 8;
arr[0][3]= 9;
arr[0][4]= 4;
arr[1][0]= 2;
arr[1][1]= 1;
arr[1][2]= 0;
arr[1][3]= 2;
arr[1][4]= 4;
arr[2][0]= 6;
arr[2][1]= 7;
arr[2][2]= 4;
arr[2][3]= 5;
arr[2][4]= 5;
arr[3][0]= 4;
arr[3][1]= 8;
arr[3][2]= 9;
arr[3][3]= 1;
arr[3][4]= 2;
arr[4][0]= 4;
arr[4][1]= 5;
arr[4][2]= 2;
arr[4][3]= 1;
arr[4][4]= 9;
//convert 2 D array in 1 D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
result[k++]=arr[i][j];
}
}
// sort 1 D array
for(i=0;i<25;i++){
for(j=0;j<24;j++){
if(result[j] > result[j+1]){
temp=result[j];
result[j]=result[j+1];
result[j+1]=temp;
}
}
}
/*
for(i=0;i<25;i++){
printf("\n%d",result[i]);
}*/
// convert 1 D array to 2 D array
i=0;
l=0;k=0;
while(i<25){
for(j=0;j<5;j++){
arr[k][j]=result[l];
l++;
}
k++;
i=i+5;
}
//Print matrix i.e 2D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
}
}
}
This works !
#define COL 5
#define ROW 2
int main(){
int temp ;
int arr[2][5]= {2,15,26,14,12,18,1,2,3,4 };
int arr2[10] = {0};
int index = 0 ;
for(int t = 0 ; t<50 ; t++ ){
for (int i =0 ; i < ROW ; i++){
for( int j = 0; j < 5-1 ; j++){
if (arr[i][j] > arr[i][j+1]){
temp = arr[i][j];
arr[i][j] = arr[i][j+1];
arr[i][j+1] = temp;
}
}
//checking for
for( int k = 0 ; k < ROW-1 ; k++){
if (arr[k][COL-1] > arr[k+1][0]){
temp = arr[k][COL-1];
arr[k][COL-1] = arr[k+1][0];
arr[k+1][0] = temp ;
}
}
//---------
}
}
return 0 ;
}
Solved here is the code that worked for me
for (i = 0; i < nbLine; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColumn) + j));
}
printf("\n");
}
I have a function that output a two dimensional array ([column][line]) and it need to be zoom, in fact it is like going from
1 2
3 4
to a
1 1 2 2
3 3 4 4 array when zoom is equal to 2
here is some code :
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, k, l;
for (i = 0; i < nbColumn; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbLine; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}
when I try this code on a squared array, it works fine, but when using a rectangular one, it fails. I have tried to debug it by replacing the printf("%d ", *(array + (i*nbColumn) + j)); by printf("%d ", (i*nbColumn) + j); and it gives me this result for a 8 colomns by 5 rows array :
0 1 2 3 4 5 6 7
5 6 7 8 9 10 11 12
10 11 12 13 14 15 16 17
15 16 17 18 19 20 21 22
20 21 22 23 24 25 26 27
Thanks for help
A working program source is:
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("in.in");
ofstream out("out.out");
int main(void){
int m[100][100],i,j,l,noLines,noColumns,zoom;
in>>noLines>>noColumns>>zoom;
for(i=0;i<noLines;i++){
for(j=0;j<noColumns;j++){
in>>m[i][j];
}
}
for (i = 0; i < noLines; i++){
for (j = 0; j < noColumns; j++){
for (l = 0; l < zoom; l++) {
out<<m[i][j]<<' ';
}
}
out<<'\n';
}
return 0;
}
On the input:
4 3 2
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
you get the output:
1 1 2 2 3 3
4 4 5 5 6 6
7 7 8 8 1 1
2 2 3 3 4 4
Changing the parameters on the first line of the input (i.e. the bidimensional array width, height and the zoom), changes the output.
From your example I can see that you are zooming only horizontally, since the elements are duplicated horizontally, but the number of rows is left intact. So you do not need vertical zoom and therefore your cycle with k is unuseful. This should work:
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, l;
for (i = 0; i < nbLine; i++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + 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 7 years ago.
Improve this question
I'm writing a Sudoku solution checker for a class and I've hit a wall.
I'm at the point where I'm checking if I can see whether or individual columns and rows are unique. For some reason the code works on 4x4 grids but as soon as I get up to a 5x5 grid or higher (goal is to get to a 9x9 grid) the program starts to print out that it had failed even when it should succeed.
Any help would be much needed, I want need a point in the right direction or where I should look into
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i, j, n, k, p, q;
int fail;
int array[5][5];
int check[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int a = 0;
char *output = NULL;
scanf("%d", &n);
// memory allocated for yes or no at end
output = malloc(sizeof(int) * (n));
while (a < n)
{
fail = 0;
// create this 2D array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &(array[i][j]));
}
}
// seeing if row is unique
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
// seeing if column is unique
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[k][j] == array[k+1][j])
fail += 1;
}
}
}
/* for (WHAT DO I DO FOR ROWS)
{
for (WHAT DO I DO FOR ROWS AGAIN BUT REPLACE ROWS WITH COLUMNS)
{
for (NOW IM LOST)
}
}
*/
// success or failure? 0 success, 1 failure
if (fail >= 1)
output[a] = 1;
else
output[a] = 0;
a++;
}
// print out yah or nah
for (i = 0; i < n; i++)
{
if (output[i] == 0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Forget my for loop for the grids, I'll work on that once I figure out how to get the columns and rows working correctly.
Thanks for the help!
Here is an input that would cause the program to fail when it should succeed
1
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
output would be
NO
EDIT: It is now working with a 9x9 grid! Thanks for the help!
#include <stdio.h>
#include <stdlib.h>
#define SIDE_LENGTH 9
int main ()
{
int i, j, n, k, p, q;
int fail;
int array[SIDE_LENGTH][SIDE_LENGTH];
int check[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int a = 0;
char *output = NULL;
scanf("%d", &n);
// memory allocated for yes or no at end
output = malloc(sizeof(int) * (n));
while (a < n)
{
fail = 0;
// create this 2D array
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
scanf("%d", &(array[i][j]));
}
}
// seeing if row is unique
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
for (k = 0; k < SIDE_LENGTH - 1; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
// seeing if column is unique
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
for (k = 0; k < SIDE_LENGTH - 1; k++)
{
if (array[k][j] == array[k+1][j])
fail += 1;
}
}
}
/* for (WHAT DO I DO FOR ROWS)
{
for (WHAT DO I DO FOR ROWS AGAIN BUT REPLACE ROWS WITH COLUMNS)
{
for (NOW IM LOST)
}
}
*/
// success or failure? 0 success, 1 failure
if (fail >= 1)
output[a] = 1;
else
output[a] = 0;
a++;
}
// print out yah or nah
for (i = 0; i < n; i++)
{
if (output[i] == 0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
input:
1
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
#ameyCU helped find the error in my code
Setting k to one less than what i and j were set to allowed the code to successfully run on any X*X sized grid. Because k is one less than i and j, it won't try to access a part of the array that hasn't been allocated yet which is where my problem lied.
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
Despite the overwriting of the array as already pointed out, your logic is flawed. You don't use j at all. You are just comparing the same values five times.
The problem is the comparison.
if (array[i][k] == array[i][k+1])
I think you are using i as row and column index, then using j to iterate for duplicates. k will be what you compare against so ...
/* compare if j'th value is same as k'th value */
if (j != k && array[i][j] == array[i][k]) /* Don't check same against same */
the second comparison should be
/* compare if j'th value is same as k'th value */
if (j != k && array[j][i] == array[k][i]) /* Don't check same against same */
That would fix your overflow (k+1) bug, and get you going.
The squares could be fixed with
struct co_ords {
int x;
int y;
};
struct co_ords boxes[][9] = {{ {0,0}, {0,1}, {0,2},
{1,0}, {1,1}, {1,2},
{2,0}, {2,1}, {2,2}
},
{ {3,0}, {3,1}, {3,2},
{4,0}, {4,1}, {4,2},
{5,0}, {5,1}, {5,2} },
... /* more boxes go here */
{ {6,6}, {6,7}, {6,8},
{7,6}, {7,7}, {7,8},
{8,6}, {8,7}, {8,8} }};
for( i = 0; i < 9; i++ ){
struct co_ords current_box * = boxes[i];
for( j = 0; j < 9; j++ ) {
for( k = 0; k < 9; k++ ){
if( j != k && array[ current_box[j].x ][ current_box[j].y] == array[ current_box[k].x ][ current_box[k].y] )
fail += 1;
}
}
}
int array[5][5];
so the array is allocated as 5x5
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &(array[i][j]));
}
}
and you are indexing from 0 to 5 ..
to use larger, please do replace all those "5"s with a precompiler definition.
#define SUDOKU_SIDE_LENGTH 5
...
int array[SUDOKU_SIDE_LENGTH ][SUDOKU_SIDE_LENGTH ];
...
for (i = 0; i < SUDOKU_SIDE_LENGTH ; i++)
{
for (j = 0; j < SUDOKU_SIDE_LENGTH ; j++)
{
scanf("%d", &(array[i][j]));
}
}
etc.. that will ensure that you always allocate enough space for the array.
adjust size on the definition, not in the code..