How to create block with specific integer with C language - c

Two-dimensional integer array (5x5) with only 0 and 1 values.
There are:
8 neighbours for any element in the middle of the matrix.
5 neighbours for the elements on the border (row=0, column=0, row=4 or column=4) of the matrix
3 neighbours for the elements on the corner of the matrix.
Block contains connected 1.
In this homework, you will write a complete C program to create a 2-D (5x5) array and fill the array with random numbers (0 or 1). Program will find the number of blocks in the matrix (5x5).
Here is my question, I fill array with numbers but I can't find how to connect 1s as block.
This is my code:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void find(int arr[][5],int size);
int main()
{
int arr[5][5];
int i,j;
srand (time(NULL));
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
arr[i][j]=rand()%2;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
find(arr,5);
}
void find(int arr[][5],int size)
{
int i,j,count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
}
}
}

a 'brute' force algorithm would use the facts as you laid out.
I.E. 4 checks for the corners 12 checks for the edges, 9 checks for the interior blocks
and example corner check:
int countOfBlocks = 0;
if( arr[0][0] && arr[0][1] && arr[1][0] && arr[1][1] ) countOfBlocks++;
...
The code would then take 25 lines of 'if's, similar to the above

Related

Dividing the array and subtracting array elements from each block: C program

I have an array having 100 numbers say a[1,2,3,4,5,6....98,99,100]. I want to divide it into 25 groups with each group containing 4 elements and then subtract elements of other blocks with each element of one block. For example: if three blocks out of 25 are labelled as A,B,C and contain elements as:
A [1,2,3,4],
B[5,6,7,8] &
C[9,10,11,12]
then subtraction is to be done like this:
(A-B, A-C),
(B-A, B-C) &
(C-A, C-B)
i.e.
1-5,1-6,1-7,1-8,1-9,1-10,1-11,1-12; then
2-5,2-6,2-6,2-8,2-9,2-10,2-11,2-12; then
3-5,3-6,3-7,3-8,3-9,3-10,3-11,3-12; then
4-5,4-6,4-7,4-8,4-9,4-10,4-11,4-12;
THEN
5-1,5-2,5-3,5-4,5-9,5-10,5-11,5-12;
and like wise..
Can anyone help me in writing C program for this.
The code that I have written is partial and not doing the above task completely. Code is:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100]={1,2,3,4.....,98,99,100};
int i=0, j=0;
int x[100], y[100];
// considering only 12 numbers for the sake of simplicity
for (i=0;i<12;i++)
{
for(j=0;j<8;j++)
{
x[j] = a[i] - a[r+4];
}
y[i] = x[i];
}
}
#include <stdio.h>
int main(void) {
int N, i;
scanf("%d", &N);
int numArray[N]; // Define an array of four integers
// Get inputs for the array elements
for (i=0;i<N; i++) {
scanf("%d", &numArray[i]);
}
int sum = 0;
// Write here the logic to add these integers:
for (i=0;i<N;i++) sum += numArray[i];
printf("%d\n",sum); // Print the sum
return 0;
}

Passing Multidimensional arrays to functions as Pointers

I have to pass two arrays ( A and B with dimensions of 5X4) to two functions called FUNCTION_1 and FUNCTION_2. Both of arrays columns and rows should be passed as POINTERS. FUNCTION_1 will take each element of A array and calculate the sum of prime factors of each element located in A( (with the help of another function called sumPrime), then it will store these sums in array B. FUNCTION_2 has to print both of A and B arrays. ( The normal numbers array, and the prime factors sums array). There are some additions in the program which are not important now but I am going to show them too in respect of clearness.
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 4
# include <math.h>
int sumPrime(int number){
int factor = 2;
int sum=0;
while(1!=number){
if(number%factor==0){
number /= factor;
sum+=factor;
factor = 2;
continue;
}
factor++;
}
return sum;
}
int FUNCTION_1(int *a[][20],int *b[][20],int row, int col){
int c,d;
for(c=0;c<row;c++){
for(d=0;d<col;d++){
b[c][d]=sumPrime(a[c][d]);
return b[c][d];
}
}
}
void FUNCTION_2(int *x[][20],int *y[][20],int rows, int cols){
printf(" \n A matrix is :\n");
int e,f;
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,x[e][f]);
}
}
printf("\n B matrix is:\n");
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,FUNCTION_1(x,rows,cols,y,rows,cols));
}
}
}
int main(){
int A[ROW][COL]={0};
int B[ROW][COL]={0};
int x=1;
int i;
int j;
for(i=0;i<ROW;i++){
for(j=0;j<COL;j++){
A[i][j]=x;
x=x+2;
}
}
printf("%d",A[0][0]);
return 0;
}
When trying to perform FUNCTION_1 or FUNCTION_2 I get many errors or even when defining the functions. There is no problem with defining A matrix or prime function! HELP!!
Your definition for a matrix is wrong ..
Use :
func(int* a[20] , int cols)
in this case you will have 20 rows ,
or
func(int a[][20] , int rows)
And define some macro for the columns if it is fixed value .

assigning all numbers in a random order into 2D array from a given list

I have written the following program in c to distribute K numbers in arandom order into M*N (M=row,N=column) array where K=MN, such that each cell gets a unique number from 0 to MN. But there is a problem. I am getting some repeated numbers into sum cells. How will I get all the unique numbers?
Sample program:#
#include<stdio.h>
#include<math.h>
#include <time.h>
#define row 5
#define col 5
int total=row*col;
int A[row][col];
main()
{
int i,j;
srand(time(NULL));
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
A[i][j]=rand()%total+1;
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nA[%d][%d]=%d",i,j,A[i][j]);
}
}
}
Notice that saying "these numbers are all different" immediately makes them less random than saying "these numbers are random". You seem to feel that duplicates makes the collection of numbers as a whole less random, which is not true.
You seem to want a permutation. One way of achieving that is by doing random shuffles, i.e. initialize the matrix with unique numbers, then repeatedly pick two cells at random and swap their contents. There are, undoubtedly, all manner of ways of knowing when you've done "enough" swaps.
Here's how one swap could be implemented:
void random_point(int *x, int *y)
{
*x = rand() % col;
*y = rand() % row;
}
void permute(void)
{
int x1, y1, x2, y2;
random_point(&x1, &y1);
random_point(&x2, &y2);
const int tmp = A[y1][x1];
A[y1][x1] = A[y2][x2];
A[y2][x2] = tmp;
}
That's very basic.

Swapping within an Array of user inputs

I am here to get some advice on how to continue my program. It is a homework assignment and the idea is to have another method called int is_sorted(int array[], int length);
With these pre and post conditions.
Precondition: array will be an array of integers of length length.
Postcondition: returns true if the array is in sorted(nondecreasing) order, or false otherwise.
So far I have been able to put together the user input array and how long it should be.
#include <stdio.h>
#include <math.h>
int is_sorted(int array[], int lenght);
int is_sorted(int array[], int lenght)
{
int swap;
int smallest;
int index = 0;
scanf("%d", &lenght);
int list[lenght];
int i;
for (i = 0; i < lenght; i++)
{
scanf("%d", &list[i]);
}
return 0;
}
int main()
{
}
How would I go about asking for a user input to swap two elements at a time within the given array?
The final product should look similar to this:
Sample Run: User input in bold
4 <- The length that the array should be.
1 1 1 2 <- user input these 4 numbers.
WHAT IS THE NEXT SWAP? 2 3
EVAN HAS UNSORTED THE ARRAY.
WHAT IS THE NEXT SWAP? 2 0
WHAT IS THE NEXT SWAP? 0 3
EVAN HAS SORTED THE ARRAY.
WHAT IS THE NEXT SWAP? -1 -1
STEVE WAS RIGHT!
-1 -1 end the swapping process and check if the array is sorted.
while(i != -1 && j != -1){
scanf("%d %d", &i, &j);
swap(&array[i], &array[j]);
}
and swap() looks like this:
void swap(int* a, int* b){
int c = *a;
*a = *b;
*b = c;
}
There's probably a nifty trick for swaping two variables doing some bitwise xor-ing that I don't remember about.

How to use functions with arrays correctly

This is a small program that I wrote to add 2 square matrices.
When i input the second matrix the values of the first are altered and the result is subsequently false. It works quite well for 2x2 matrices,But not for any bigger matrix.
The algorithm works well without the use of functions.
Please help me solve this problem.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int n;
void PrintMatrix(int P[n][n])
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t", P[i][j]);
}
}printf("\n");
}
void ReadMatrix(int P[n][n])
{
int i,j;
for(i=0;i<n;i++)
{
printf("input row %d\n", i+1);
for(j=0;j<n;j++)
{
scanf("%d", &P[i][j]);
}
}
}
void AddMatrix(int P[n][n], int Q[n][n], int Result[n][n])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Result[i][j]=P[i][j]+Q[i][j];
}
}
}
int main()
{
int i,j;
int A[n][n];
int B[n][n];
int Sum[n][n];
printf("input matrix size:");
scanf("%d", &n);
printf("input first matrix\n");
ReadMatrix(A);
PrintMatrix(A);
printf("input second matrix\n");
ReadMatrix(B);
PrintMatrix(B);
AddMatrix(A,B,Sum);
PrintMatrix(Sum);
return 0;
}
You can define your own matrix type with a struct, such as for example:
typedef struct
{
int* data;
size_t width;
size_t height
} matrix_t;
Or if you have a modern C compiler, you can write the functions like
void func (size_t width, size_t height, int matrix[width][height])
But the struct version is probably to prefer.
EDIT
As for why your program is buggy, you must initialize n to a value. If you declare the arrays as variable-length arrays of size [n][n] after the point where you read n from the user, it should work fine.
if you run this under gdb here is something you'll find
(gdb) p A
$14 = 0x7fffffffdfb0
(gdb) p B
$15 = 0x7fffffffdfa0
irrespective of whatever "n" you choose the array base address always differ by 16 that is integer 2 x 2 matrix works
but if i Input length as 3 the program crashes. There is memory corruption.
before doing input
(gdb) x/12 A
0x7fffffffdfb0: 0 0 -255260739 52
0x7fffffffdfc0: -1 -1 -1 -1
0x7fffffffdfd0: -8272 32767 -1 -1
after input:
x/12 0x7fffffffdfb0
0x7fffffffdfb0: 1 2 3 4
0x7fffffffdfc0: 5 6 7 8
0x7fffffffdfd0: 9 32767 -1 -1
is fine as i gave values 1 2 3 4 5 6 7 8 9
but
p A
$16 = 0x7fff00000009
is this accessing it causes segmentation fault in PrintMatrix.
now if you change your program to this
int main()
{
int i,j;
printf("input matrix size:");
scanf("%d", &n);
int A[n][n];
int B[n][n];
int Sum[n][n];
your problems are solved and you are good to go

Resources