I write this function but when I print the array a get incorrect numbers:
the problem is because the MAX_SIZE, how can I solve this without change MAX_SIZE?
#include <stdio.h>
int MAX_SIZE = 3;
void print(int arr[MAX_SIZE][MAX_SIZE],int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
printf("%d ", arr[i][j]);
}
int main()
{
int arr[][2] = {{1, 2}, {4, 5}};
print(arr,2); // 1 2 5 0
return 0;
}
First of all, you should declare constants in C like:
#define MAX_SIZE <value>
Your problem will be solved if you set MAX_SIZE to 2 since you're trying to access arr[][2] not arr[][3].
Correct syntax:
#define MAX_SIZE 2
Output:
1 2 4 5
That's it.
Related
Two integers are stored in the arrays a 1 and a 2, respectively, and the product is calculated by the same procedure as the calculation, but it does not output the correct result.
question is : want to produce 312*321 = 1 0 0 1 5 2 but this first program produce
? 0 9 9 11 5 2
to produce right result 1 0 0 1 5 2, call function name func(c,N*2)
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
The result:0 9 9 11 5 2
|0|1|2| ->A1
----------------
A2<-| 0|2|4|6|
| 1|1|2|3|
| 2|3|6|9|
this array is the same as 321*312 calculate using hand in paper
Problem: Define the function func () to output the correct result 1 0 0 1 5 2, call func (c, N * 2); below i post the code with the call function func() in bold. any idea?? and also what the logic behind func()? trial and error? is there algorithm behind this?
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
**func(c,N*2);**
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
**void func(int a[],int digit)
{
here no idea....
}**
Try with this;
void func(int a[], int digit)
{
int i, c = 0;
for(i = 0; i < digit; i ++)
{
a[i] += c;
c = a[i] / 10;
a[i] = a[i] % 10;
}
}
I think you should change two things:
First initialization the result array
int c[N * 2] = {0}; //initialize
Also the function looks like
void func(int a[], int size) {
int carry = 0;
for (int i = 0; i < size; i++) {
a[i] += carry;
carry = a[i] / 10;
a[i] = a[i] % 10;
}
}
I have an array of 6 rows and 20 columns :
char name[6][20];
And I enter the names with for :
puts("Enter with 6 names :");
for(i=0; i< 6 ; i++)
{
scanf("%19[^\n]%*c",name[i]);
}
After that, I need to randomly choose three names of the array and display them in the screen. How can I do that ?
PS : Different from the other questions similar to that, I want not to take just one word, but the full word of the array.
Here's a possible solution to your problem, assuming you've stored the array of names, just create an array of positions and then shuffle it few times so the positions will be random ones, finally pick 3 positions (for instance, the first 3 ones):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 6
#define COL 20
#define RND_NAMES 3
void shuffle(int *array, int n, int num_shuffles) {
srand((unsigned)time(NULL));
for (int j = 0; j < num_shuffles; j++) {
for (int i = 0; i < n - 1; i++) {
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
int main() {
int i;
char name[ROWS][COL] = {"name1", "name2", "name3",
"name4", "name5", "name6"};
int positions[ROWS] = {0, 1, 2, 3, 4, 5};
shuffle(positions, ROWS, 100);
printf("%s\n", name[positions[0]]);
printf("%s\n", name[positions[1]]);
printf("%s\n", name[positions[2]]);
return 0;
}
With this way, you're guaranteed to pick up 3 random non-repeated names.
Here I wrote a simple solution for what you're trying to achieve.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 6
#define COL 20
#define RND_NAMES 3
int main()
{
int i;
char name[ROWS][COL];
// init pseudo-random number generator
srand((unsigned)time(NULL));
puts("Enter with 6 names: ");
for (i=0; i < ROWS; i++) {
scanf("%19[^\n]%*c", name[i]);
}
puts("Random names: ");
for (i=0; i < RND_NAMES; i++) {
printf("%s\n", name[rand() % ROWS]);
}
return 0;
}
I want to produce different numbers with C.
We can generate a random number using the stdlib library and the srand function.
For example; I want to produce a random number between 0 and 5.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
int i;
int n = 4;
int array[3];
srand(time(NULL));
for(i = 0; i < n; i++)
{
array[i] = rand() % 5;
printf("%d\n", array[i]);
}
return 0;
But the same numbers may coincide here.Like this:
2
4
4
1
How can I prevent this?
Maybe you can use something like this:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
int i;
int n = 4;
int array[4];
// Fill an array with possible values
int values[5] = {0, 1, 2, 3, 4};
srand(time(NULL));
for(i = 0; i < n; i++)
{
int t1 = rand() % (5-i); // Generate next index while making the
// possible value one lesser for each
// loop
array[i] = values[t1]; // Assign value
printf("%d\n", array[i]);
values[t1] = values[4-i]; // Get rid of the used value by
// replacing it with an unused value
}
return 0;
}
Instead of random number you can generate random non-zero shift from the previous number:
#include <stdio.h>
#include <stdlib.h>
int myrand() {
static int prev = -1;
if (prev < 0)
prev = rand() % 5;
prev = (prev + 1 + rand() % 4) % 5;
return prev;
}
int main(void) {
int i;
for (i = 0; i < 20; i++)
printf("%d\n", myrand());
}
How to pass a 2D array as a parameter in C?
I was searching to pass a 2d array to a function in c and I came across the above site. I understood the first and second way of passing 2d array, but I got
confused in the 3rd method, specifically, how is it even working that way?`
3) Using an array of pointers or double pointer
In this method also, we must typecast the 2D array when passing to function.
#include <stdio.h>
// Same as "void print(int **arr, int m, int n)"
void print(int *arr[], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3;
int n = 3;
print((int **)arr, m, n);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
`
The above code works fine on codeblocks.
When calling print() from main(), we pass arr as argument by typecasting it to pointer to pointer , but in the function print() it dereferences only once to print the values. printf("%d ", *((arr+i*n) + j));
Shouldn't it be *((*arr+i*n) + j));, I tried compiling this statement , it compiles but doesn't execute.
2) Using a single pointer
In this method, we must typecast the 2D array when passing to function.
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print((int *)arr, m, n);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
`
The 2nd method and the 3rd method only differ in the type of argument passed in the print() function while rest of the code is same. So what is actually the difference between the working of the functions?
easiest way to pass 2D array,
function
void PrintArray(unsigned char mat[][4]){
int i, j;
printf("\n");
for(i = 0;i<4;i++){
for(j = 0;j<4;j++)
printf("%3x",mat[i][j]);
printf("\n");
}
printf("\n");
}
main
int main(){
int i,j;
//static int c=175;
unsigned char state[4][4], key[4][4], expandedKey[176];
printf("enter the value to be decrypted");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%x",(unsigned int *)&state[j][i]);
PrintArray(state);
return 0;
}
Let's say we have an array of m elements and we want to change randomly the position of exactly n of them, where of course 2 <= n <= m.
For example: if we have this array of 10 ints {1 2 3 4 5 6 7 8 9 10} and we ask for 4 of its elements to change positions randomly, a result could be {3 2 1 4 5 6 10 8 9 7}
What is the simplest way to program this in ANSI C? (pseudocode will also be just fine)
Step1). Generate a list of n random unique numbers between 1 and m. This list should NOT be sorted. Eg, for your example, the list could have been [10,7,1,3]
Step 2) do something like :
int save = array[list[0]];
For (i=0; i<n-1; i++) {
Array[list[i]] = array[list[i+1]];
}
Array[list[n-1]] = save;
Edit: actually, you'll have to have subtract 1 from each list[whatever] to allow for zero-based arrays - but I'm sure you get the idea :)
since at least two numbers must be swapped, i would pick two random numbers from array first and then swap them. they are added to array that holds swapped indexes. if there's more to swap, pick another number different than swapped ones and swap it with one of the previously swapped numbers.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print_arr(int a[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ",a[i]);
}
printf("\n");
}
void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int next_idx(int swapped[], int s_count, int size) {
int n, i;
char in_arr;
while (1) {
in_arr = 0;
n = rand() % size;
for (i = 0; i < s_count; i++) {
if (swapped[i] == n) {
in_arr = 1;
break;
}
}
if (!in_arr) {
break;
}
}
return n;
}
void swap2_or_more(int a[], int size,int count) {
srand(time(NULL));
int i, j, s_count = 0;
int swapped[size];
i = rand() % size;
swapped[s_count] = i;
s_count++;
do {
j = rand() % size;
} while (i == j); // make sure indexes are different
swapped[s_count] = j;
s_count++;
swap(a, i, j);
count -= 2;
while (count) {
i = next_idx(swapped, s_count, size);
j = rand() % s_count;
swap(a, i, swapped[j]);
swapped[s_count] = i;
s_count++;
count--;
}
printf("swapped indexes:\n");
print_arr(swapped, s_count);
}
int main(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
swap2_or_more(a, 10, 5);
printf("array after swap:\n");
print_arr(a, 10);
return 0;
}