Conditional rand - arrays

this code gives a 4x3 array of numbers from 2 to 100:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LINES 4
#define COLUMNS 3
int main(void) {
int matriz[LINES][COLUMNS];
int i, j;
srand(time(NULL));
for(i = 0; i < LINES; i++){
for(j = 0; j < COLUMNS; j++){
matriz[i][j] = rand() % 99 + 2;
}
}
printf("Matriz[%i][%i]:\n", LINES, COLUMNS);
for(i = 0; i < LINES; i++){
for(j = 0; j < COLUMNS; j++){
printf("\t%i", matriz[i][j]);
}
printf("\n");
}
return 0;
}
what can I do so this array only receive EVEN numbers from the rand function?

Related

I want to create an array of 100 size, with unique random integers from 1 to 999999 in C

I want to create an array of 100 size, which its elements are unique random integers from 1 to 999999. My code doesn't give any error message or the output that I want. What is wrong with this?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
#define EMPTY -1
int main() {
srand(time(NULL));
int list[999999], A[N], i;
for (i = 0; i < N; i++)
A[i] = EMPTY;
for (i = 0; i < 999999; i++) {
list[i] = i + 1;
}
for (i = 0; i < 999999; i++) {
int j = rand() % 999999;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
for (i = 0; i < N; i++) {
A[i] = list[i];
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
}
The initial loop is useless, the method is very inefficient, but the output should meet the goal...
Yet there might be an issue with the list array: it is very large and defining it as a local variable with automatic storage cause a stack overflow, depending on your target system. Try defining is as:
static int list[999999];
Here is an alternative for N small compared to 1000000:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
int main() {
int A[N], i, j;
srand(time(NULL));
for (i = 0; i < N;) {
int n = 1 + rand() % 999999;
for (j = 0; j < i; j++) {
if (A[j] == n)
break;
}
if (i == j)
A[i++] = n;
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
return 0;
}

Determine if a rectangular matrix has two rows of positive elements

I need to determine if a rectangular matrix has two rows of positive elements in C. I write part code for the set matrix and output its elements. I don't know how to check the positive elements in the row. Please help me
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main() {
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++) {
printf("%d line:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
_getch();
return 0;
}
So, I make some changes in my code after reading comments. Thanks a lot. But it's not working.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //обьявление матрицы 3 строки и 4 столбца
int i, j; // индексы строки и столбца
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}
What you need to do is to set a counter to 0 . Every time all the values are positive in row add 1 your counter. You can do that by a loop. If a value of your matrix a[i][j] is less than 0 just continue and move on to then next row.

C language 2d array fill diagonal with numbers from 1 to n

I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;

(SOLVED) C 2D Array: Finding Max from each row and Min from each column

I am creating a program to print out the maximum value of each row and the minimum value of each column of an NxN 2D array.
So, for example I have this 4x4 2D array:
1 14 11 16
9 10 3 12
13 6 15 8
13 2 7 4
Then, the output should be
16 12 15 13 //Max of each row
1 2 3 4 //Min of each column
EDIT: Here is the revised code that works.
#include <stdio.h>
#include <stdlib.h>
void getMax(int n, int a[][n])
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int n, int b[][n])
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(r,a);
printf("\n");
getMin(r,a);
printf("\n");
}
This is my OLD code that did not work. Turns out I don't have to use MAX. Replace the a[][MAX] to a[][n].
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void getMax(int a[][MAX], int n)
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int b[][MAX], int n)
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(a,r);
printf("\n");
getMin(a,r);
}
Can anybody point out what I did wrong?
Thank you!

Calculations using random numbers of a specific range in a 2D array in c

I have to populate a 2D array using random numbers between 3 and 19.
The array is 4 x 3, the first two columns will represent two sides to a right triangle and the third column is the hypotenuse.
I'm pretty new to C, so I'm not sure where I'm going wrong. my output gives me the headers just fine but just one single vertical line of numbers instead of a 4 x 3 grid.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define ROW 4
#define COL 3
void printChart(double array[ROW][COL]);
int main(void)
{
double chart[ROW][COL];
double *ptrchart = &chart[0][0];
srand(time(NULL));
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
chart[i][j] = 3 + (rand() % 19);
}
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = 0;
}
printChart(ptrchart);
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = (double)sqrt(pow(chart[i][0], 2) + pow(chart[i][1], 2));
}
puts(" ");
printChart(ptrchart);
return 0;
}
void printChart(double array[ROW][COL])
{
printf("%s", "Side A\tSide B\tHypotenuse(Side C)\n");
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
printf("%.3f\t", array[i][j]);
if (j = 2)
{
puts(" ");
}
}
}
}
Any help would be greatly appreciated, and if I need to clarify anything please let me know.
you should just put (j==2) instead of (j=2) in your function since it changes j to always be 2 the way you wrote it
I ran the below code and it works
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define ROW 4
#define COL 3
void printChart(double array[ROW][COL])
{
printf("%s", "Side A\tSide B\tHypotenuse(Side C)\n");
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
printf("%.3f\t", array[i][j]);
if (j == 2)
{
puts(" ");
}
}
}
}
int main(void)
{
double chart[ROW][COL];
double *ptrchart = &chart[0][0];
srand(time(NULL));
for (size_t i = 0;i < ROW;i++)
{
for (size_t j = 0;j < COL;j++)
{
chart[i][j] = 3 + (rand() % 19);
}
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = 0;
}
for (size_t i = 0;i < ROW;i++)
{
chart[i][2] = (double)sqrt(pow(chart[i][0], 2) + pow(chart[i][1], 2));
}
puts(" ");
printChart(ptrchart);
return 0;
}

Resources