Changing the sign of array values in C - c

I need to write a function that takes the elements in an array and changes the sign (ex. 3 --> -3 or -3 --> 3). l want use this array (int a[2][3] = { { 55,-44,},{1, -4},{6,11} };) instead of ( int a[] = { 5,6,-4};)
What should I do?
#include <stdio.h>
void change_sign(int* beta)
{
for (int i = 0; i < 3; i++) {
beta[i] = -beta[i];
}
}
int main(void)
{
int a[] = { 5, 6, -4};
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
printf("\n");
change_sign(a);
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
return 0;
}

For starters it seems you mean this array
int a[3][2] = { { 55,-44,},{1, -4},{6,11} };
instead of this
int a[2][3] = { { 55,-44,},{1, -4},{6,11} };
In any case if your compiler supports variable length arrays then the function can look like
void change_sign( size_t m, size_t n, int a[][n] )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and call the function like
change_sign( 3, 2, a );
Otherwise the function can look like
#define N 2
void change_sign( int a[][N], size_t m )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and called like
change_sign( a, 3 );

Related

Common elements within 2 arrays

I'm trying to write a function that copies all of the values in source1 which are also found in source2 into a destination and then returns the number of elements copied into the destination.
int common_elements(int length, int source1[length], int source2[length], int destination[length])
{
int counter = 0;
int i = 0;
while (i < length) {
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
i++;
}
return counter;
}
The problem is e.g. given (common_elements(5, {1,2,3,4,5}, {1,2,3,2,1}, [])), the correct input should be
1,2,3
return value: 3
However, the program is accounting for the duplicates and produces :
1,1,2,2,3
return value: 5
which is incorrect.
How can I remedy this?
In this while loop
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
you are counting all elements in the array source2 that are equal to the element source1[i].
I can suggest the following solution provided that the source arrays may not be changed within the function.
#include <stdio.h>
size_t common_elements( int destination[],
const int source1[],
const int source2[],
size_t n )
{
size_t counter = 0;
for ( size_t i = 0; i < n; i++ )
{
size_t number = 1;
for ( size_t j = 0; j < i; j++ )
{
if ( source1[i] == source1[j] ) ++number;
}
for ( size_t j = 0; number && j < n; j++ )
{
if ( source1[i] == source2[j] ) --number;
}
if ( number == 0 ) destination[counter++] = source1[i];
}
return counter;
}
int main(void)
{
enum { N = 5 };
int source1[N] = { 1, 2, 3, 4, 5 };
int source2[N] = { 1, 2, 3, 2, 1 };
int destination[N];
size_t n = common_elements( destination, source1, source2, N );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", destination[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3

2d / multidimensional array char

Here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
int i,k;
char a[4][2] = { {'*','*'}, {'*','*'}, {'*','*'}, {'*','*'}};
/* output each array element's value */
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 2; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
return 0;
}
I would like to know how to replace a character from a 2d array with another character with user input? for example if the user wants to replace the asterisk at [0][0] with an F the output would look like this:
F *
* *
* *
* *
`
I would really appreciate it because I can't seem to find any example of this anywhere. Thanks
int main ()
{
int i,k,row,column;
char a[4][2] = { {'*','*'}, {'*','*'}, {'*','*'}, {'*','*'}},rc;
// before replace
printf("Before Replace :\n");
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 2; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
printf("Enter a Character you want to Replace : ");
scanf("%c",&rc);
printf("Enter row and column Index: ");
scanf("%d%d",&row,&column);
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 2; k++ ) {
if(i==row && k==column){
a[i][k]=rc;
}
}
}
printf("\nAfter replace :\n");
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 2; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
return 0;
}

I want to find all sub matrix of a matrix. I made a code in C, but it only finds square ones. How can I improve my code

The following code finds all square sub matrix of a square matrix:
#include <stdio.h>
int main() {
int mtrx_size = 3;
int mat[3][3] = {
{ 1, 2, 3},
{ 9,10,11},
{17,18,19},
};
//I took 3*3 matrix for example though It works for any square matrix
int i, j, k, l, m;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
m = mtrx_size - sub_mtrx_size + 1;
for (k = 0; k <m; k++) {
for (l = 0; l <m; l++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+k][j+k]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
But I want to find all of the sub matrix of a matrix.
I've tried this way to get all my sub matrix:
#include <stdio.h>
int main() {
int mtrx_size = 3;
int mtrx_size1=3;
int mat[3][3] = {
{ 1, 2,3},
{ 9,10,4},
{ 5,6,7},
};
//Here I took example for 3*3 matrix though it should work for others
int i, j, ioff, joff, off_cnt,off_cnt1;
int sub_mtrx_size,sub_mtrx_size1;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 0 ; sub_mtrx_size--)
for(sub_mtrx_size1 = mtrx_size1; sub_mtrx_size1 > 0 ; sub_mtrx_size1--){
off_cnt = mtrx_size - sub_mtrx_size + 1;
off_cnt1 = mtrx_size1 - sub_mtrx_size1 + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt1; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size1; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

How to print all square submatrices of square matrix in C?

Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language
I wrote code that works wrong:
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 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,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i,j;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
{
for(i = 0; i < sub_mtrx_size; i++)
{
for(j = 0; j < sub_mtrx_size; j++)
{
printf("%3d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.
Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 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,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
Java implementation for a general nxm matrix:
private static void printSubMatrix(int[][] mat) {
int rows=mat.length;
int cols=mat[0].length;
//prints all submatrix greater than or equal to 2x2
for (int subRow = rows; subRow >= 2; subRow--) {
int rowLimit = rows - subRow + 1;
for (int subCol = cols; subCol >= 2; subCol--) {
int colLimit = cols - subCol + 1;
for (int startRow = 0; startRow < rowLimit; startRow++) {
for (int startCol = 0; startCol < colLimit; startCol++) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
System.out.print(mat[i + startRow][j + startCol] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
}
}
}
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 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,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

Bubble sort an Array of pointers of another arrays in a function (C)

I want to make a bubble sort function of an array of pointers that each of the pointers point to another arrays - inside of function and i'm getting a error that i violated a writing location (Visual Studio)
P.S, I do (*parr)++ because the first value of each array shows the length of the array without the first value so i need to start bubble sorting from the second box (arr[1] and not arr[0] for example ).
can someone write to me how can i fix it?
Thanks for help
(I need to sort the values of the original arrays not the pointer of the arrays).
int main(void){
int i = 0;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
system("PAUSE");
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++, (parr)++)
{
length = **parr;
(*parr)++;
for (j = 0; j < length-1; j++)
{
for (k = 0; k < length - j - 1; k++, (*parr)++)
{
if ((**parr)>(*(*parr + 1)))
{
temp = **(parr);
**(parr) = (*(*parr + 1));
(*(*parr + 1)) = temp;
}
}
}
}
}
This seems to work. It is easier in func1 to use dereferenceing as parr[i][k] rather than moving the pointer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func1(int** parr);
int main(void){
int j;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
for (j = 1; j <= arr0[0]; j++)
{
printf ( "arr0[%d] %d\n", j, arr0[j]);
}
for (j = 1; j <= arr4[0]; j++)
{
printf ( "arr4[%d] %d\n", j, arr4[j]);
}
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++)
{
length = **parr;
for (j = 0; j < length; j++)
{
for (k = 1; k < length - j; k++)
{
temp = *((*parr)+k);
if (*((*parr)+k)>*((*parr)+k+1))
{
temp = *((*parr)+k);
*((*parr)+k) = *((*parr)+k+1);
*((*parr)+k+1) = temp;
}
}
}
*parr++;// next array
}
}

Resources