Pointers with multidimensional arrays in c [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i have a function that generates randomize numbers between 0 and 2,and i want to use pointers but anyway i could not executed it.
First i generate randomize number and then i want to print the multidimensional array to see what it has generated
i just want to assign randomized number to an array(with using pointers) and then print them using pointers again
int **RandomizedMatrix; //integer multidimensional array
int ***AdressOfMatrix = &RandomizedMatrix; //assigning the adress of matrix to an integer pointer
void RandomizedMatrixGenerator(int LineNumber,int ColumnNumber,int ***AdressOfMatrix)
{
int i,j;
for (i=0; i<LineNumber*ColumnNumber; i++)
{
**AdressOfMatrix = rand()%2;
}
for (i=0; i<LineNumber; i++)
{
for (j=0; j<ColumnNumber; j++)
{
printf("%d",***(AdressOfMatrix+i)+j);
}
printf("\n");
}
}

If what i understand is right you want to fill your 2 dimensional array with 1s and 0s so you can do that
void randmtrgen(int row,int column,int **mtr){
int i,j;
for (i=0;i<row;i++){
for (j=0;j<column;j++){
mtr[row][column]=rand()%1+1;
}
}
}
That will fill your matrix as you wish and i suggest you to not use dereference operator if you are not good with it square brackets mostly fine.
And if you have an urge to use dereference operator you can use that line
*(*(mtr+row)+column)=rand()%1+1;

OK, so you want to assign random numbers you need two for() loops:
for (i=0; i<LineNumber*ColumnNumber; i++)
{
**AdressOfMatrix = rand()%2;
}
All this is going to do is assign LineNumber*ColumnNumber number of times the first element of your matrix a number. If you want to do it this way, you need to increment the rows and columns while assigning.
Also your "random" number isn't going to be random because you didn't seed it. Here's an example of what you wanted to do:
srand((unsigned) time(NULL)); // Seed the random number generator
for(row = 0; row<LineNumber; row++){
for(col = 0; col<ColumnNumber; col++){
ran = rand()%100; // Generate a randome numer 1-99
*(*(matrix+row)+col) = ran; // assign a random number to the element
}
}
for(row = 0; row<LineNumber; row++) {
for(col = 0; col<ColumnNumber; col++){
printf("[%d]", *(*(matrix+row)+col)); //Display the random number
}
printf("\n");
}
So given a 4x3 matrix, you could get an output like:
[99][33][38]
[59][2][28]
[50][39][2]
[85][45][99]

Related

Convert 2D array to single array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
suppose i have a two D array.
#define ROWS 3
#define COLS 3
char a[ROWS][COLS]= {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
How can I copy values of that to a single array. I want only COLS values.
1) If you need to copy the array in row wise order
you could use :
-first have a 1D array that could hold all the elements of 2D array, then use
memcpy(new1Darr, org2Darr, total size in bytes);
like for above example,
memcpy(new1Darray, a, sizeof(char)*ROWS*COLS)
2) Instead of (1) or if you want to change the order in which the data must be stored then just traverse through the 2D array the way you want(column major) and copy the elements one by one. Like (considering you define all the variables first)
This will copy the elements in the new array in column wise order
k=0;
for(j=0;j<COLS;j++)
{
for(i=0;i<ROWS;i++)
{
new1Darray[k] = a[i][j];
k++;
}
}
Its very simple. Lets see 'how?'-
#define ROWS 3
#define COLS 3
#include<stdio.h>
#include<conio.h>
char a[ROWS*COLS]={'1','2','3','4','5','6','7','8','9'};
void main()
{
for(int i=0; i<ROWS*COLS;i=i+COLS)
{
for(int j=0; j<COLS; j++)
{
printf("%c\t",a[i+j]); //access array
}
printf("\n");
}
getch();
}

Multiplying a matrix and a vector in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to multiply a matrix and a vector, I already wrote the functions to store the matrix and vector in malloc arrays. For this function, I need to create another array using malloc to store my answer first. then do the calculations (http://www.facstaff.bucknell.edu/mastascu/elessonsHTML/Circuit/MatVecMultiply.htm)
#include <stdlib.h>
/* Multiply a matrix by a vector. */
double *matrix_vector_multiply(int rows, int cols,
double **mat, double *vec){
// creating an vecotr to hold the answer first
double *ans= malloc(rows* sizeof(double));
// do the multiplication:
mulitply **mat and *vec (mat = the matrix and vec is the vector)
for (rows=0; rows< ; rows++)
for (cols=0; cols< ; cols++)
ans[rows] = ans[rows] + vec[rows][cols] * mat[rows];
//not sure if it is right
// then store the answer back to the ans array
}
the main function:
double *matrix_vector_multiply(int rows, int cols,
double **mat, double *vec);
int main(){
double *answer = matrix_vector_multiply(rows, cols, matrix, vector);
printf("Answer vector: \n");
print_vector(rows, answer);
return 0;
}
not sure how to do this multiplication with pointers and then storing it back..
any help would be appreciated! thanks!
edit: the multiply function:
#include <stdlib.h>
/* Multiply a matrix by a vector. */
double *matrix_vector_multiply(int rows, int cols,
double **mat, double *vec){
double *ans = malloc(rows * sizeof (double));
int i;
for (i=0; i<rows; rows++)
for (i=0; i<cols; cols++)
ans[rows] = ans[rows] + vec[rows][cols] * mat[rows];
return ans;
}
but i am getting a error at line 12, subscripted value is either array nor pointer
There are several mistakes in your functions:
the variables for iterating through the matrix in the for loops shouldn't be the arguments you passed to your function. Try with something like this:
for(y=0;y<rows;y++)
you have to swap vec and mat in the for loops
you will have to initialize your answer vector to 0 (another for loop)
you have to return the answer (return ans;) at the end of your multiplication
Hope that helps,
Jan

2d arrays and image smoothing in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to write a program that " will take in an image as input, represented by 2d array pixel values(for simplicity, each pixel can be represented by an integer). Output the resulting smoothed image by applying the mean filter to every pixel in the array".
I'm just learning about arrays, but I am lost as to how to even start this program. Whenever I search this topic it gets very confusing because every example or concept that I find is using or talking about actual images. Since my program is using integers, I'm having a hard time distinguishing what is needed and what is not. Basically I understand the premise(at least i think I do), each number must take the mean value from the 4 numbers around it, but outside the basic concept, I'm at a loss as to what needs to be done. Any help, suggestions, or examples would be great.
thanks
A key concept:
int values[20][20];
float intermediates[20][20];
for (int y = 1; y < 19; y++)
for (int x = 1; x < 19; x++)
intermediates = (float)values[y][x];
int means[20][20];
for (int y = 1; y < 19; y++)
for (int x = 1; x < 19; x++)
means[y][x] = (int) ( (float) (intermediates[y-1][x-1] + intermediates[y-1][x] + intermediates[y-1][x+1] + intermediates[y][x-1] + intermediates[y][x] + intermediates[y][x+1] + intermediates[y+1][x-1] + intermediates[y+1][x] + intermediates[y+1][x+1]) / 9.0F); // Divisor is 9 because we added nine values and we're getting the mean
Now there are 4 corner cases: intermediates[0][0], intermediates[0][19], etc. and then all the sides.
The values were copied into "intermediates" first because I didn't want to put (float)in front of everything.
0) Load the image? or are you given the integer array?
1) We start with a 2D array, generate a copy: int I[N][M]; int New[N][M];
2) Loop through the values:
for (i = 0; i < N; i++){
for (j = 0; j < M; j++){
3) Do your stuff
New[i][j] = sum(I[i-1][j] + I[i][j-1]...)/4;
note, beware (i or j-1) going < 0 and (i or j+1) > N or M. deal with these boundary conditions as you see fit.
Obviously this is the simplest way, but it looks like you're in a class so it should be fine

findind the median of n integers from user [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Does anyone know how to change the code below from enthusiasticgeek to allow for user input (e.g. Input n, and then n integers)? Output these n integers and the median. n is odd and positive and is less than 1 million.
Code pasted from enthusiasticgeek.com:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 6
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, ELEMENTS, sizeof(int), compare);
for (n=0; n<ELEMENTS; n++)
{ printf ("%d ",values[n]); }
printf ("median=%d ",values[ELEMENTS/2]);
return 0;
}
use:
int values[static or dynamic allocation dependent]
for (i=0;i<n;i++){
printf("Enter number:");
scanf(%d, value[i]);
}
instead of just hardcoding the values for the values array.
Make sure you either dynamically declare the array, or statically make it large enough to accept the number of integers you want, 1,000,000 but thats a ton of memory. No one's going to take that much time to enter that many numbers manually. Probably safe with declaring it at 30 tops.
Or if you're planning to use the "define elements" part, you only need to declare the values array to be 6.

convert from 1d to 2d matrix using arithmetic pointer in c language [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how can i convert 1 dimension to 2 dimensional's matrix addressing and 1 d block addressing with pointer arithmetic in c programming language.
can anybody help please and thanks in advance:)
Suppose you want a 10x15 matrix (10 rows, 15 columns), for allocating memory:
int numRows = 10;
int numColumns = 15;
int *matrix = malloc(sizeof(int) * numRows * numColumns);
To access any element, multiply the desired row by the number of columns and add the column number:
//access row 3, column 5:
int value = matrix(3 * numColumns + 5);
Thats it.
int** matrix = malloc(sizeof(int*) * COLUMN_SIZE);
matrix[0] = malloc(sizeof(int) * COLUMN_SIZE * ROW_SIZE);
int* temp = matrix[0];
for (int i=1; i < COLUMN_SIZE; i++)
{
matrix[i] = temp;
temp += ROW_SIZE;
}
Now you'll have a linear array in matrix[0] that can also be addressed using either a linear index like matrix[0][INDEX] where index is between 0 and (COLUMN_SIZE * ROW_SIZE), or you can address it using matrix[COLUMN_INDEX][ROW_INDEX].
When the linear 1d index is called index and the 2d indexes row and col, the formula to transfer between them is
index = row * (MAX_ELEMENTS_PER_ROW) + col;
and the other direction
row = index / MAX_ELEMENTS_PER_ROW;
col = index % MAX_ELEMENTS_PER_ROW;
The pointer arithmetic is done by the + on a pointer and [] operator.
The actual coding I give to you as homework ;-).

Resources