Moving parts right in two - dimensional matrix - c

I would like to get your help to understand and finish my program.
This is what I have to do:
"You must exercise program that:
First. An absorbing two - dimensional integer arr [M] [N]. M - number of rows N - number of columns. (Matrix size was received from the user)
Two. The program uses auxiliary functions "shift" moves the values ​​of the matrix to the right one place, as shown in the picture (2 entered instead of 1, 3 instead of 2, 4 instead of 3, ... 20 instead of 19, first place 20).
Shift have to write a function and call her three times in the sample matrix loop .."
Example
My problems are:
I don't know how to do the matrix two - dimensional integer arrays that there size is entered by the user. I only know by DEFINE SIZE of the row and the cols
My function isn't close to the real deal so I would like to get help finish my function.
My output:
My code:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define M 4
#define N 5
void shift (int arr[M][N], int length);
void main()
{
int arr[M][N];
int i,j,length;
printf("Enter %d rows \n",M);
for (i=0 ; i<M ; i++ )
{
printf("Enter %d numbers:\n",N);
for(j=0 ; j<N ; j++ )
{
scanf("%d" , &arr[i][j] );
}
length=N+M;
}
shift (arr,length);
system("pause");
return ;
}
void shift (int arr[M][N], int length)
{
int i,j,temp;
temp=arr[0][0];
for(i=0; i<M; i++)
{
for(j=0; j<N-1 ; j++)
{
printf("%d ",arr[i][j]);
}
arr[i][j]=temp;
printf("\n");
}
}
Edit: pictures resized

Shifts all columns to the right.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void shift_columns_right(int M[100][100], int rows, int cols) {
int tmp_lastcol;
int j, k;
for (j = 0; j<rows; j++){
tmp_lastcol = M[j][cols-1];
for (k = cols-1; k > 0; k-- ){
M[j][k] = M[j][k-1];
}
M[j][0] = tmp_lastcol;
}
}
int main(void){
int B[100] [100] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20},
};
shift_columns_right(B,5,4);
return 0;
}

I will you give a hint as how to shift the elements. The logic is to swap the elements between the current and last element in the row as you iterate. I will show you a working example on 1D array.
#include <stdio.h>
#define ARRAY_SIZE 5
int main()
{
int a[ARRAY_SIZE] = {11,22,33,44,55};
int i;
for (i=0; i<ARRAY_SIZE; ++i)
{
int temp = a[i];
a[i] = a[ARRAY_SIZE-1];
a[ARRAY_SIZE-1] = temp;
}
for(i=0; i<ARRAY_SIZE; ++i)
{
printf("%d\t",a[i]);
}
return 0;
}
Output: 55 11 22 33 44
To dynamically allocate the memory for array, use malloc. Hope it helps !

Related

Program in c array

textI have to make one array which is A[n][n]. In this array the user puts some numbers and from this array I have to make a second array which is C[n] which numbers are positive number by row from first one. I try do it by this way but in that way it works for every numbers it have to be only by rows .have to look something like that for example
A00:1 A01:-3 A02:5
A10:-7 A11:-8 A12:7
A20:6 A21:9 A22:10
C00:2 C01:1 C02:3
But in my way it look :
C00:3 C01:3 C02:3 which is wrong.
If somebody have an idea where is the problem I will be so thankful.
> const int n=10;
int a[n][n];
int c[n];
int i,j,k,suma;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (i=0;i<n;i++){
for (j=0;j<n;j++){
do{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
while(a[i][j]<-1000||a[i][j]>1000);
}
}
suma=0;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j]>0){
suma=suma+1;}
}
}
for(k=0;k<n;k++){
printf("c[%d]=%d",k,suma);
}
return 0;
}
Main changes:
Populate array c[] and discard variable suma. This was the primary issue with your code.
You declare n as a const but then allow user to enter its value. I've removed the const and used a #define to set the max dimensions for the arrays.
Removed while loop - what's this for?
General tidying up.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10
int main() {
int a[MAX][MAX] = {0};
int c[MAX] = {0};
int n = MAX;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (int i=0;i<n;i++)
for (int j=0;j<n;j++) {
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
c[i] += a[i][j] > 0 ? 1 : 0;
for(int k=0;k<n;k++)
printf("c[%d]=%d\n",k,c[k]);
return 0;
}

How can I create a multidimensional array of random integers printed using a function?

Baby programmer here! I am stumped. I need help with an assignment and as concluded in my last question, my professor is no help, and I have done lots of research and I cannot find any examples or answers in my book or YouTube. In C, I have to use a loop to load random integer values into a 1 dimensional array and a multidimensional array, and use functions to print the contents of each array. I've taught myself how to load random integers into a 1 dimensional array, but I'm having trouble figuring out how to call it to a function to print the results. Similarly, I can create a multidimensional array and print the results through a function, but I can't figure out how to make the integers in a multidimensional array random.
Here is what I've created and understand so far (It is a MESS and I thank you for patience in advance):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printArray(int a[], size_t size); //prototype
void printArray(int b[2][3]); //prototype
int main(void)
{
int n[10]; // n is an array of 10 integers
int i;
srand(time(NULL)); //uses time to make integers random
printf("One-Dimensional Array\n");
for (i = 0; i < 10; i++) //loop 10 times
{
n[i] = i + 1;
printf("%d ", (rand() % 50) + 1); //print random int from 1-100
}
printf("\nMulti-Dimensional Array\n");
int array1[2][3] = { {1, 2, 3}, {4, 5, 6} };
printArray(array1);
}
void printArray(int a[], size_t size)
{
//insert function for printing 1d array
}
void printArray(int b[][3])
{
for (size_t i = 0; i <= 1; ++i)
{
for (size_t j = 0; j <= 2; ++j)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
}
Use nested loops, just like you do when printing the array.
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
array1[i][j] = rand() % 50 + 1; // random int from 1 to 50
}
}

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work?

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work? After filling it all the numbers should be printed.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=1; i<=10; i++)
{
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}
In C this first index is 0. Therefore the code should use indexes 0 through to 9
I.e.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=0; i<10; i++)
{
number[i] = 1 + i;
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}

How to randomly pick elements from an array in C?

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

Initializes an array by a given size in random values between 1 and 500 in c

I have assignment to write program that sort an array and search for a specific number, that part I've already done, My problem is how to Initialize the array in the size that the user sets with random values ​​smaller than 500? I know how to do that with known size but not with unknown size?
example for input/output:
"Please enter the size of the array:
5"
This will help you.
int main(void)
{
int n,i;
n=rand()%500; // Get random value
int arr[n]; // Initialize the dynamic array
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
// Do your stuff
}
You can do something like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printArray(int* p, int size)
{
for (int j = 0; j < size; ++ j) printf("%d ", p[j]);
printf("\n");
}
int main(void) {
srand(time(NULL)); // Start with a random seed based on time
int n = 0;
printf("Which array size do you need?\n");
if (scanf("%d", &n) != 1 || n < 1)
{
printf("Wrong input\n");
exit(0);
};
printf("Creating random array of size %d\n", n);
int* p = malloc(n * sizeof(*p)); // Reserve memory
for (int j = 0; j < n; ++j) p[j] = rand() % 500; // Generate random numbers
printArray(p, n); // Print the result
free(p); // free the allocated memory
return 0;
}

Resources