I'm trying to check if the inputted array of the user is mirrored or not
Users input:
//for the rows and column
3 3
//elements inside array
1 0 1
2 0 3
4 0 4
the output should tell if the first index and the last index of the array is the same, but the only thing that i have done is mirror it and print mirror even if its not the same
int main()
{
int arr[10][10];
int col,row;
scanf("%d %d", &col, &row);
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
scanf("%d", &arr[i][j]);
}
}
for (int i = 0; i < col; i++) {
for(int j = row - 1; j >= 0; j--) {
printf("Mirror");
}
}
}
the size of the array and the elements depends on the user, thank youuu
This is my solution for your problem, this code can have some isuess but in general do what you ask for. Adapt for your particular needs.
OUTPUT:
This program will check if an array is mirrored. Press enter to proceed!
Insert row*col -> 3*3
Insert row 1 elements, one by one!
Element 1 -> 1
Element 2 -> 0
Element 3 -> 1
Insert row 2 elements, one by one!
Element 1 -> 2
Element 2 -> 0
Element 3 -> 3
Insert row 3 elements, one by one!
Element 1 -> 4
Element 2 -> 0
Element 3 -> 4
Mirror found at row 1!
1 0 1
Mirror found at row 3!
4 0 4
CODE:
#include <stdio.h>
#define MAXCOLUMN 10
#define MAXROW 10
void clean(){
int temp;
while ((temp = getchar()) != '\n' && temp != EOF);
}
void print(int dim,int r, int row[][MAXCOLUMN]){
for(int index = 0; index < dim; index++)
printf("%d ",row[r][index]);
putchar('\n');
}
int main(void){
int arr[MAXROW][MAXCOLUMN];
int col,row,mirror;
puts("This program will check if an array is mirrored. Press enter to proceed!");
do{
clean();
printf("Insert row*col -> ");
}
while(scanf("%d*%d", &row, &col)!=2);
for(int r = 0; r < row; r++){
printf("Insert row %d elements, one by one!\n",r+1);
for(int c = 0; c < col; c++){
do{
clean();
printf("Element %d -> ", c+1);
}while(!scanf("%d",&arr[r][c]));
}
}
for(int r = 0; r < row; r++){
mirror = 1;
for(int cFH = 0, cSH = col-1; cFH < col/2; cFH++, cSH--)//cFH -> column index first half | cSH -> column index second half
if(arr[r][cFH] != arr[r][cSH]){
mirror = 0;
break;
}
if(mirror){
printf("Mirror found at row %d!\n",r+1);
print(col,r,arr);
}
}
return 0;
}
Usually you iterate through the columns of each row, that means that usually i (the outer loop) represents the rows and j the columns.
This piece of code shows a way to check if a 2D array is mirrored horizontally:
int mirror = 1;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col/2; j++) {
// comparing first element with last element of row `i`
// than comparing the second element with the second last
// and so on, till you reach the center of the row, `j==col/2`
// if the comparison is true just one time, the array is not mirrored
if( array[i][j] != array[i][col-j-1] ) {
mirror = 0;
i = row; // to break the outer loop
break;
}
}
}
mirror ? puts("Mirror!") : puts("No mirror.");
At the end of the program if the variable mirror still have a value of 1, you got a mirror array.
Related
Here's the question my teacher gave me
Write a C program to store 10 integers in an array of size 10 and
display the contents of the array. Replace the highest 5 numbers in the array by
1 and lowest 5 numbers by 0 and display the contents of the new array.
[For e.g.
Original Array
44 11 6 99 30 78 32 31 66 55
New Array
1 0 0 1 0 1 0 0 1 1
I have been struggling in this question whole day :(
There are a lot of ways to solve this problem. A good way would be sort the array into another array and then replace the 1st half with 0s and the second half with 1s like this:
#include<stdio.h>
int main(){
const int arraySize = 10;
int i, j;
int arr[arraySize];
int arrSorted[arraySize];
int temp;
// Get input from user
printf("Please enter 10 numbers!\n");
for (i = 0; i < arraySize; i++)
{
scanf("%d", &arr[i]);
// Copy array into another to sort it later
arrSorted[i] = arr[i];
}
// Print input
printf("Input: ");
for (i = 0; i < arraySize; i++)
{
printf("%3d ", arr[i]);
}
printf("\n");
//Sort the array in ascending order
for (i = 0; i < arraySize; i++)
{
for (j = i + 1; j < arraySize; j++)
{
if(arrSorted[i] > arrSorted[j])
{
temp = arrSorted[i];
arrSorted[i] = arrSorted[j];
arrSorted[j] = temp;
}
}
}
// Start replacing procedure
for (i = 0; i < arraySize; i++)
{
for (j = 0; j < arraySize; j++)
{
if (arr[j] == arrSorted[i])
{
if (i < arraySize / 2) // Replace 1st half with 0s
{
arr[j] = 0;
}
else // Replace 2nd half with 1s
{
arr[j] = 1;
}
break;
}
}
}
// Print result
printf("Result: ");
for (i = 0; i < arraySize; i++)
{
printf("%3d ", arr[i]);
}
printf("\n");
return 0;
}
Of course, you can use the C standard library qsort() function if you don't want to sort yourself.
Another solution would be, find the median number of the array then replace any number which is less than it with 0 and any number bigger than it with 1. Although with this solution there will be some challenges regarding what to do with the median number itself and what if there are multiple median numbers (duplicated)?
I want to output Yay if the matrix doesn't contain the same number on the same row or column otherwise output Nay
this is for my college homework. I already tried to check the column and row in the same loop but the output still not right
#include <stdio.h>
int main()
{
int size;
int flag;
scanf("%d",&size);
char matrix[size][size];
for (int i = 0; i < size; i++)
{
for (int l = 0; l < size; l++)
{
scanf("%s",&matrix[i]);
}
}
for (int j = 0; j < size; j++){
for (int k = 0; k < size; k++){
if(matrix[j] == matrix[j+1] || matrix[j][k]==matrix[j][k+1])
{
flag = 1;
}
else
{
flag = 0;
}
}
}
if (flag == 1)
{
printf("Nay\n");
}
else
{
printf("Yay\n");
}
return 0;
}
I expect to output "Nay" when I input
3
1 2 3
1 2 3
2 1 3
and "Yay" when i input
3
1 2 3
2 3 1
3 1 2
Your matrix is a 2D array and you are referencing it using only a single subscript matrix[index] at several places which returns the address of the row. Index it using both the row and column indices. Try the code below:
{
int size;
int flag;
scanf("%d",&size);
char matrix[size][size];
for (int i = 0; i < size; i++)
{
for (int l = 0; l < size; l++)
{
scanf("%s",&matrix[i][l]);
}
}
for(int j = 0; j < size; j++){
for (int k = 0; j < size; j++){
if(matrix[j][0]== matrix[j][k] || matrix[k][0]==matrix[k][j])
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
}
if (flag == 1)
{
printf("Nay\n");
}
else
{
printf("Yay\n");
}
return 0;
}
Your have a logic problem. Your flag is reset on every element in the matrix and thus only reflects the result of the last check.
In addition, you need a break; in your nested loop. The logic is, if your flag becomes 1, you are sure to say Nay, and you don't want the flag to be reset to 0.
int flag = 0;
for (int i = 0; i != size && !flag; ++i) {
for (int j = 0; j != size; ++j) {
if ( /* detection */ ) {
flag = 1;
break;
}
}
}
if (flag)
printf("Nay\n");
else
printf("Yay\n");
Note: The commented /* detection */ part requires more work. Since it's your homework, you may try it first. You could use a hash table for memorization. Or brutal force to make the program simply work. It seems that your detection only checks for neighboring elements, which is not sufficient to assert that an element is unique in its row or column. Consider
1 2 1
3 4 5
6 7 8
I can't do your homework for you. The following is the brutal-force way you may consider.
The if ( /* detection */ ) part could be if (has_same_element(matrix, i, j)), with a function (pseudo code)
int has_same_element(matrix, row, col)
{
for each element a in matrix's row except matrix[row][col] itself
if (a == matrix[row][col])
return 1
for each element b in matrix's col except matrix[row][col] itself
if (b == matrix[row][col])
return 1
return 0
}
Of course there are smarter ways, like using a hash table, in which case you don't even need the nested loop. For the time being, work out a feasible solution, instead of the best solution.
My task is to print out a matrix of integer numbers scanned from the keyboard, and then to print out the numbers under the second diagonal using separate functions. My code looks like this:
#include <stdio.h>
#include <stdlib.h>
//array[n][n] = 2D array of n rows and n columns
//n = number of rows and columns
void printmatrix(int n, int array[n][n]){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i][j]);
}
printf("\n");
}
return;
}
void undersdiagonal(int n, int array[n][n]){
for(int i = 1; i < n; i++){
/**/
for(int j = 1; (j < n); j++){
printf("%d ", array[i][j]);
}
}
return;
}
int main(){
int n;
scanf("%d", &n);
int integermatrix[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &integermatrix[i][j]);
}
}
printf("The entered matrix is:\n");
//calls first function to print matrix
printmatrix(n, integermatrix);
printf("Under the secondary diagonal:\n");
//calls second function to print numbers under main diagonal
undersdiagonal(n,integermatrix);
printf("\n");
return 0;
}
I can't figure out how to print out the numbers under the second main diagonal from the function
void undersdiagonal(int n, int array[n][n]).
My input looks like this:
3
1
2
3
4
5
6
7
8
9
And the output looks like this:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
5 6 8 9
But I want the output to be:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
6 8 9
What could I change in the 'for' loop within the 'for' loop in the function
void undersdiagonal(int n, int array[n][n])
?
The key is with the inner loop of undersdiagonal:
As currently written, for(int j = 1; (j < n); j++){ starts with the second column (column 1) every single time. It needs to start on a different column for each row.
For a 3×3 matrix, the second row (row 1) will begin on the last column (column 2). For a 4×4 matrix, the second row (row 1) will begin on the last column (column 3)... So there is a direct relationship between the starting column (j) and the matrix size: n.
Every time the row number goes up one, the starting column goes down one. So there is an inverse relationship between the starting column (j) and the row: - i.
Take those two pieces together and you have int j = n - i;
I am trying to display the elements of an array in the number of columns that the user specifies. They decide how large the array is and how many columns it will be displayed in. Right now, I am printing the elements of the array in columns but, I have extra rows and columns with numbers that are not in the array. Thus if I select array size and column number as 5 3, I would hope to see:
1 3 5
2 4
Instead, I get something like:
1 3 5
2 4 107863456
128976543 58764 896543221
5643217 90876543456 8976543
I am getting 3 columns with 4 rows. I do not know why this is happening. Here is the portion of my code that deals with creating columns, let me know if more code is needed (x is variable that holds array size, y holds number of columns):
void colDisplay(int *aPtr, int x, int y) {
int i, j;
srand(time(NULL));
for(i = 0; i < x; i++) {
aPtr[i] = rand()%5+1;
}
/*Trying to format display for number of columns used*/
printf("Unsorted columns\n");
for(i = 0; i < x; i++) {
for(j = 0; j < y; j++) {
//printf("%d = %d ", (i*y)+j, aPtr[(i*y)+j]);
printf("%d ", aPtr[(i*y)+j]);
}
printf("\n");
}
}
The inner loop is counting the columns correctly, but the outer loop is using x as a row count, instead of an item count. To fix the problem you can use a single loop that counts items, and outputs newlines at the correct times.
j = 0;
for ( i = 0; i < x; i++ )
{
printf("%d ", aPtr[i] );
j++;
if ( j == y || i == x-1 )
{
printf( "\n" );
j = 0;
}
}
I agree with the solution by #user3386109.
Also, the following change will help:
Original 'for' loop with j:
for(j = 0; j < y; j++)
Modified code:
for (j = 0; (j < y) && (i*y+j < x); j++)
Reason: index = i*y+j may exceed x if (x % y != 0) i.e. if x (array size) is not integral multiple of y (display column size).
Because of arrays index out of bound.
You should do the following:
for(i = 0; i >= 0; i++) {
boolean isContinue = true;
for(j = 0; j < y; j++) {
int index = i*y+j;
if(index==x){
isContinue = false;
break;
}
printf("%d ", aPtr[(i*y)+j]);
}
if(!isContinue){
break;
}
printf("\n");
}
I'm trying to make a matrix algebra program, so I began with a pre defined 3x3 matrix. Ran into some issues with the elements not being read properly into the 2D array so I isolated one . The issue is that the last number in a row is printed as the first element in the next row. i.e. array[1][2] is printed out as what array [2][0] is.
Again, if I have a 3x3 matrix where we go from 1 to 9, and I want the value at row 1 column 3 (which is 3), it'll give me 4. Same goes for row 2 column 3; the value is 6 but it gives me 7.
I've put in a print statement to show me the counter values before input and they're correct. Something is going on with the scanf I guess but I'm really stumped. Any help would be appreciated!
#include <stdio.h>
int matrix1[2][2];
void main(void)
{
int i = 0, j=0; //Row and column subscripts for the first matrix
//======================================================================================================//
//First Matrix
printf("This is for the first matrix");
for(i=0; i < 3; i++) //Start with row
{
for(j = 0; j < 3; j++)
{
printf("\nInsert the value at row %d, column %d: ", i+1,j+1); //+1 since array's begin at 0
printf("\ni is %d, j is %d", i,j); //Shows what elements the input will go into
scanf("%d", &matrix1[i][j]); //Enter value into row i column j
}
}
printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
}
#include <stdio.h>
#define ROWS 3
#define COLS 3
int matrix1[ROWS][COLS];
void main(void)
{
int i, j; //Row and column subscripts for the first matrix
//================================================================================
//First Matrix
printf("This is for the first matrix");
for(i=0; i < ROWS; i++) //Start with row
{
for(j = 0; j < COLS; j++)
{
printf("\nInsert the value at row %d, column %d: ", i+1,j+1);
printf("\ni is %d, j is %d", i,j);
scanf("%d", &matrix1[i][j]);
}
}
printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
}
I think You are confused with array index numbers and total number of elements in array.
For Example When You have array of 5 integers.
int ary[5] = {11,22,33,44,55};
`------------IN Memory-----------
| 11 || 22 || 33 | | 44 | | 55 |
------------------------------- `
0 1 2 3 4 <--------These are called Index numbers.
They always start with 0,Hence
they always end with(Total Number of elements - 1)
Hence From above explanation you either want to change the size of array
from int matrix[2][2] to int matrix[3][3];
OR
either change your for() loops to
for(i=0; i < 2; i++) //changed i<3 to i<2
{
for(j = 0; j < 2; j++) //changed i<3 to i<2
{
// your Code.....
}
}