2d / multidimensional array char - arrays

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;
}

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

Changing the sign of array values in 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 );

How to sort an array in descending order

I have this code but this is not showing me the required result. that is to merge 2 arrays and print it in descending order. I wan to merge 2 sorted arrays taking input from user. user will tell the size of array the elements of array too and then my program should merge and sort descendingly and print
int main()
{
int num1,num2,i,tem;
printf("Number of elements in first array:");
scanf("%d",&num1);
printf("Number of elements in second array:");
scanf("%d",&num2);
int array1[num1],array2[num2],merge[num1+num2];
printf("Elements for array 1 \n");
for ( i = 0; i < num1; i++)
{
printf("Element:");
scanf("%d",&array1[i]);
}
printf("Elements for second array\n");
for ( i = 0; i < num2; i++)
{
printf("Element:");
scanf("%d",&array2[i]);
}
for ( i = 0; i < num1; i++)
{
merge[i] = array1[i];
}
for ( i = 0; i < num2; i++)
{
merge[i+num1] = array2[i];
}
for ( i = 0; i < num1 + num2; i++ )
{
if ( merge[i] < merge[i+1] )
{
tem = merge[i];
merge[i] = merge[i+1];
merge[i+1] = tem;
}
}
printf("Merge:");
for ( i = 0; i < num1 + num2; i++ )
{
printf("%d ",&merge[i]);
}
return 0;
}
int count1 = 10;
int count2 = 15;
int arNums1[];
int arNums2[];
int arMergeNums[];
arNums1 = new int[count1];
arNums2 = new int[count2];
arMergeNums = new int[count1 + count2];
//POPULATE YOUR FIRST TWO ARRAYS HERE...
//FILL YOUR MERGED ARRAY LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
if ( i < count1 )
arMergeNums[i] = arNums1[i];
else arMergeNums[i] = arNums2[i];
}
//THEN SORT IT LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
for (int j = i + 1; j < (count1 + count2); j++)
{
if (arMergeNums[i] < arMergeNums[j])
{
int temp = arMergeNums[i];
arMergeNums[i] = arMergeNums[j];
arMergeNums[j] = temp;
temp = null;
}
}
}
That's it...
$(document).ready(function(){
var cars = [4,3,9,6];
for(i=0;i<cars.length;i++){
//var carsrev=cars[cars.length-i-1];
//alert(carsrev);
$('<li>'+cars[cars.length-1-i]+'</li>').appendTo("ul.demo");
//alert($("ul.demo li").length);
}
});
//Out Put is 6 9 3 4 not 9 6 4 3 its just reverse

Pass values using function

this code is supposed to be a person walking to right in a 4x4 array filled with '*'. for example: if the person walks to the right R ,row zero and walks one step it would look like this:
O * * *
* * * *
* * * *
* * * *
or row one, walks two steps
* * * *
* O * *
* * * *
* * * *
row three, walks four steps
* * * *
* * * *
* * * *
* * * O
this is the original code:
#include <stdio.h>
int main ()
{
int i,k, c, f;
char z;
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}};
printf("Walking to the right\n");
z=getchar();
// modified array goes here []
if(z=='R')
{
printf("Row\n");
scanf("%d",&c);
printf("Steps");
scanf("%d",&f);
a[c][f]='O';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return ;
}
I would like to have the values for R, row and steps be passed using a function to int main where only the array above is printed.
I tried doing something but I don't really know where to go from here. :/
#include <stdio.h>
void print(char);
int main ()
{
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}};
printf("From the right\n");
z=getchar();
// modified array goes here []
void print(char z){
int i,k;
int c, f;
if(z=='R')
{
printf("Row\n");
scanf("%d",&c);
printf("Steps");
scanf("%d",&f);
a[c][f]='O';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return ;
}
#include <stdio.h>
int main ()
{
int i,k,x,c,f;
char z;
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'},{'*','*','*','*'}, {'*','*','*','*'}};
printf("From the right\n");
z=getchar();
printf("Row\n");
scanf("%d",&c);
if(z=='R')
{
printf("Steps");
scanf("%d",&f);
a[c][f]='G';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return 0;
}
I'll appreciate any suggestions. Thanks.
Unclear however look at this:
void getCommand(char &z) {
printf("Walking to the right\n");
z = getchar();
}
void getRowAndSteps(char z, int &c, int &f) {
if (z == 'R')
{
printf("Row\n");
scanf("%d", &c);
printf("Steps");
scanf("%d", &f);
}//else...
}
void print(char a[][4]) {
for (size_t i = 0; i < 4; i++) {
for (size_t k = 0; k < 4; k++) {
printf("%c ", a[i][k]);
}
printf("\n");
}
}
int main()
{
int c, f;
char z;
char a[4][4] = { { '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' } };
getCommand(z);
getRowAndSteps(z, c, f);
a[c][f] = 'O';
//maybe some cycle here for each step ??
print(a);
return 0;
}
or:
int main(int argc, char * argv[])
{
if (argc != 4) {
printf("Usage: .exe side row steps");
return 0;
}
int c = atoi(argv[2]), f = atoi(argv[3]);
char z = argv[1][0];
char a[4][4] = { { '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' } };
a[c][f] = 'O';
//maybe some cycle here for each step ??
print(a);
return 0;
}
untested, hope it will help you

Spliting string to an array in C

int main()
{
int longNum = 12345, tempNum[5], i;
for (i = 0; i <= 5; i++)
{
tempNum[i] = longNum[i] ; // not valid, how do i make this work?
}
printf("%d\n", tempNum);
return 0;
}
Im trying to go through all the digits of longNum and push them into tempNum[].
You may try the modulo operator:
int main()
{
int longNum = 12345, tempNum[5], i;
for (i = 4; i >= 0; i--)
{
tempNum[i] = longNum % 10;
longNum /= 10;
}
for( i = 0; i < 5; i++ )
{
printf("%d\n", tempNum[i]);
}
return 0;
}
For a dynamic length:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long longNum = 1234512345; // can be any size within `long long` limit.
int tempNum[5], i;
int num_digits = 0;
long temp = longNum;
while( temp > 0 )
{
temp /= 10;
num_digits++;
}
printf( "num_digits = %d\n", num_digits );
// Allocate dynamic array.
int *pos = malloc( num_digits * sizeof( int ) );
for( i = num_digits - 1; i >= 0; i-- )
{
pos[i] = longNum % 10;
longNum /= 10;
}
for( i = 0; i < num_digits; i++ )
{
printf("%d\n", pos[i]);
}
return 0;
}
You could try this:
int main( )
{
int longNum = 12345, tempNum[5], i;
char numstr[99];
itoa( longNum, numstr, 10 );
for ( i = 0; i < 5; i++ )
{
tempNum[i]=numstr[i] - '0';
printf( "\n%d", tempNum[i] );
}
return 0;
}
#include <stdio.h>
int main()
{
int longNum = 12345, tempNum[5], i;
for (i = 1; i <= 5; i++)
{
tempNum[5-i] = longNum % 10;
longNum = longNum/10;
}
printf("%d%d%d%d%d\n", tempNum[0], tempNum[1], tempNum[2], tempNum[3], tempNum[4]);
return 0;
}

Resources