function to fill the top part of a table - c

I have a function that allows me to fill the top part of a table by a number like in this example with 1:
2 3 3 4
2 3 3 4
2 3 4 5
3 4 2 4
becomes after applying the function :
2 1 1 1
2 3 1 1
2 3 4 1
3 4 2 4
This is my code (using an if test):
/* function */
void remplitPartieSup(int tab[N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (i<j)
tab[i][j]=1;
}
}
}
Question
How can I achieve the same result without using an if test?

Try this.
void remplitPartieSup(int tab[N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++)
{
tab[i][j]=1;
}
}
}

Related

MPI Gatherv ordering data incorrectly when different 2-D array sizes

I'm trying to take a N*N 2-D array, have each process be responsible for a number of columns, carry out an action on the elements and gather them back together into a single 2-D array again.
I have managed to divide the columns among the processes, carry out the action and bring them back together using MPI subarrays and Gatherv. However, when I give the program a number of processes that doesn't equally divide into the number of columns, the returned data is misplaced.
With the master matrix being 12x12, I provide four processes and get the correct result back:
FINAL MATRIX
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
1 1 1 2 2 2 3 3 3 4 4 4
When the matrix is still 12x12 and I provide five processes, I get this output:
FINAL MATRIX
1 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 3 3 4 4 5 5
5 1 1 2 2 2 0 0 0 0 0 0
1 1 1 2 2 2 0 0 0 0 0 0
1 1 1 2 2 2 0 0 0 0 0 0
1 1 1 2 2 2 0 0 0 0 0 0
Can someone inform me as to what I've configured incorrectly for this to be the result? Ultimately, after resolving this, I wish to switch the Gatherv to Allgatherv so that each process has the entire 2-D array locally for further alterations.
Update (11/04/2021)
As suggested by Gilles I have attempted to use column vectors instead but could not find a way in which to recombine with Gatherv. I believe my issue with my current solution may be due to displacements as manually altering these causes changes in the output (populating some of the zero cells).
Full code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void print_matrix (double ** X, int rows, int cols)
{
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j)
printf ("%.0f ", X[i][j]);
printf ("\n");
}
}
double **alloc_2d_array(int m, int n) {
double **x;
int i;
x = (double **)malloc(m*sizeof(double *));
x[0] = (double *)calloc(m*n,sizeof(double));
for ( i = 1; i < m; i++ )
x[i] = &x[0][i*n];
return x;
}
void main(int argc, char *argv[]) {
int n = 12;
int ndims = 2;
int rank, size;
int root_rank = 0;
MPI_Datatype sendsubarray, recvsubarray, resizedrecvsubarray;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Report active to console
printf("Rank: %d, reporting!\n", rank);
// Make master matrix
double ** master_matrix = alloc_2d_array(n, n);
// Set starting values in master matrix
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
master_matrix[i][j] = 0;
}
}
// Calculate sub matrices no. of columns and displacements
int interval, modulus, section_end, section_start, section_length;
int counts[size];
int displs[size];
interval = n/size;
modulus = n % size;
for (int i=0; i < size; i++) {
if (modulus != 0) {
counts[i] = interval+1;
modulus--;
} else {
counts[i] = interval;
}
displs[i] = (i == 0) ? 0 : displs[i-1]+counts[i-1];
}
// Calculate subarray info
int master_size[2] = {n, n};
int subsize[2] = {n, counts[rank]};
int startat[2] = {0, displs[rank]};
// Populate sub matrix in main matrix
for (int i = startat[0]; i < startat[0] + subsize[0]; i++)
for (int j = startat[1]; j < startat[1] + subsize[1]; j++)
master_matrix[i][j] = rank + 1;
// Print adjusted matrix
// printf("ADJUSTED MATRIX\n");
// print_matrix(master_matrix, n, n);
// Create the subarray type for use by each send node (incl. the root):
MPI_Type_create_subarray(ndims, master_size, subsize, startat, MPI_ORDER_C,
MPI_DOUBLE, &sendsubarray);
MPI_Type_commit(&sendsubarray);
// Create the subarray type for use by the receive node (the root):
if (rank == 0) {
MPI_Type_create_subarray(ndims, master_size, subsize, startat, MPI_ORDER_C,
MPI_DOUBLE, &recvsubarray);
MPI_Type_commit(&recvsubarray);
MPI_Type_create_resized(recvsubarray, 0, 1 * sizeof(double),
&resizedrecvsubarray);
MPI_Type_commit(&resizedrecvsubarray);
}
// Gather the send matrices into the receive matrix:
MPI_Gatherv(master_matrix[0], 1, sendsubarray,
master_matrix[0], counts, displs, resizedrecvsubarray,
0, MPI_COMM_WORLD);
if (rank == 0) {
printf("FINAL MATRIX\n");
print_matrix(master_matrix, n, n);
}
MPI_Finalize();
}

Printing all permutations of n numbers

Print all n! permutations of the number 1,2,3,...,n.
Example: Input: 3
Output: 1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Following is my approach. My program is not working for inputs greater than 3. I understand the logic why it is not working , but I am unable to translate that logic into a code block to overcome that issue.
#include <stdio.h>
int permute(int n)
{
int a[n];
int i,j,k,store;
for(i=0;i<n;i++)
a[i]=i+1;
for(i=1;i<=n;i++)
{
for(j=0;j<n-1;j++)
{
store=a[j+1];
a[j+1]=a[j];
a[j]=store;
for(k=0;k<n;k++)
printf("%d ",a[k]);
printf("\n");
}
}
}
int main()
{
int n;
scanf("%d",&n);
permute(n);
return 0;
}
Following is the output for n as 4:
We can clearly see that some permutation are missing, and I know exactly the fault in my code. But I am unable to fix it.( I am a beginner , hence I don't know much advanced C libraries or functions)
One solution consists in calling the function recursively: you set the first number (n possible choices), then call the function for a size n-1.
Output, for n=4
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
#include <stdio.h>
#include <stdlib.h>
void swap (int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
void permute(int index, int* arr, int n) {
if (index == n-1) {
for (int k = 0; k < n; ++k) {
printf ("%d ", arr[k]);
}
printf ("\n");
return;
}
for (int i = index; i < n; i++) {
swap (arr + index, arr + i);
permute (index+1, arr, n);
swap (arr + i, arr + index);
}
return;
}
int main()
{
int n;
if (scanf("%d",&n) != 1) exit (1);
int arr[n];
for (int i = 0; i < n; ++i) arr[i] = i+1;
permute(0, arr, n);
return 0;
}

Pattern print from a given single integer

Given a number — 4 — I have to output
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
The main code I wrote has an error for direction. According to me, I wrote the correct code, but it only works for 1 and 3 and loops between it — 1 3 1 3 1 3.... it is supposed to work 0 1 2 3 0 1 2 3 0 1.... accordingly in a spiral manner. I would really appreciate if you could help me!
The code I wrote is as follows:
int main()
{
int n;
scanf("%d", &n);
int x=n+(n-1);
int size=x*x;
int t,b,l,r;
t=0;l=0;
b=x-1;
r=x-1;
int i,j;
int res[size];
int index=0;
int dir=0;
while(t<=b && l<=r){
if(dir==0){
for(i=l;i<r;i++){
res[index]=n;
index++;
}
t++;
dir=1;
}
else if (dir==1) {
for(i=t;i<b;i++){
res[index]=n;
index++;
}
r--;
dir=2;
}
else if (dir==2) {
for(i=r;i>l;i--){
res[index]=n;
index++;
}
b--;
dir=3;
}
else if(dir==3){
for(i=b;i>l;i++){
res[index]=n;
index++;
}
l++;
dir=0;
}
n--;
dir=(dir+1)%4;
// printf("%d",dir); if u want to check
}
/* yet to proceed
for(i=0;i<x;i++){
for(j=i;j<x;j=j+x){
printf("%d ",res[j]);
}
printf("\n");
}
*/
return 0;
}

how to use recursion to count down after counting up

I am trying to get my program to count down after counting up to ten. I have tried to alter the code from counting up to make it count down to no avail.
#include <stdio.h>
void count(int k)
{
if (k > 0) {
count(-k + 1);
printf("%d", k);
}
else {
if (k == 0)
{
printf("%d,", k);
}
else {
count(k + 1);
printf("%d,", -k);
}
}
}
int main(int argc, char ** argv)
{
count(10);
getchar();
return 0;
}
Here is a simple example of the recursion which does this, illustrating Eugene's comment:
#include <stdio.h>
void count(int n) {
if (n > 10) {
printf("\n");
return;
}
printf("%d ", n);
count(n+1);
printf("%d ", n);
}
int main() {
count(0);
printf("\n");
return 0;
}
it counts up on the way into recursion and counts down while it exits it. Actually on the way down it only re-prints the state which it was before diving into the next level:
0 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1 0
The function can be easy implemented if to use a static local variable inside it. For example.
#include <stdio.h>
void count(unsigned int n)
{
static unsigned int m;
printf("%u ", m);
if (n != m)
{
++m;
count(n);
--m;
printf("%u ", m);
}
}
int main( void )
{
const unsigned int N = 10;
unsigned int i = 0;
do
{
count(i);
putchar('\n');
} while (i++ != N);
return 0;
}
The program output is
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0
Within the function the static variable m behaves as an index in a for loop (or there will be more suitable a do-while loop).
At first it is initialized implicitly by zero (as any static variable)
static unsigned int m;
You can use the initializer explicitly if you want
static unsigned int m = 0;
then it is changed from 0 to n and afterward backward from n again to 0.
++m; // changing from 0 to n
count(n);
--m; // changing from n to 0

Trying to create a function that sorts rows of 2D array using bubble sort and then prints it

I am trying to sort a 2D array using bubble sort and then print the newly sorted array. This is what I have so far:
void sort_2Darray(int x[][MAX],int size)
{
int i;
int j;
int hold;
int pass;
for(pass=1;pass<size;pass++)
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(x[i][j]>x[i][j+1])
{
hold=x[i][j];
x[i][j]=x[i][j+1];
x[i]j+1]=hold;
}
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%2d", x[i][j]);
}
printf("\n");
}
}
}
And this is what it prints when size==4
before sorting:
0 3 6 6
2 9 7 0
4 1 1 1
7 0 2 6
after it printed this 4 times:
0 3 6 6
2 7 0 0
1 1 1 4
0 2 6 -352439155
for(pass=0;pass<size;pass++)
{
for(i=0;i<(size-1);i++)
{
for(j=0;j<(size-i-1);j++)
{
if(x[pass][j]>x[pass][j+1])
{
hold=x[pass][j];
x[pass][j]=x[pass][j+1];
x[pass][j+1]=hold;
}
}
}
I haven't tested this code, but this should work properly and do the job, I think.
Please report if it doesn't work.

Resources