Hackerrank small triangles, large triangles problem - c

Link
here is link to question. In this question we have to sort the triangles based on their areas and then print out the dimensions of triangle in sorted format.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
void sort_by_area(triangle* tr, int n) {
/**
* Sort an array a of the length n
*/
double arr[n+1];
triangle temp;
for(int i=0;i<n;i++)
{
double area_2,p;
p=((tr[i].a+tr[i].b+tr[i].c)/2.0);
area_2=(p*(p-tr[i].a)*(p-tr[i].b)*(p-tr[i].c));
arr[i]=area_2;
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=tr[i];
tr[i]=tr[j];
tr[j]=temp;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}
This is my code. Only sample testcase is getting passed with this code and all else testcases are wrong. can someone pls help me with this?

Thanks a lot guys for your help. finally with lots of debugging, i understood the error in my code. while swapping the structure array i was forgetting to swap the area array as well.Here is the code for this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
#define longlong int;
void swap(double *array,int i,int j)
{
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}
void sort_by_area(triangle* tr, int n) {
double arr[n];
triangle temp;
for(int i=0;i<n;i++)
{
double area_2,p;
p=((tr[i].a+tr[i].b+tr[i].c)/2.0);
area_2=(p*(p-tr[i].a)*(p-tr[i].b)*(p-tr[i].c));
arr[i]=area_2;
/*storing square of area in a different array of different types of triangle*/
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
/*using bubble sort comparing the area of subsequent triangles and if area of subsequent triangles are found to be greater than the previous one then swapping the areas as well as the structure array*/
{ swap(arr,i,j);
temp=tr[i];
tr[i]=tr[j];
tr[j]=temp;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}

Related

Swapping Two Structs in C (Dynamic Memory Allocation)

I am trying to swap two structs in C. The issue is I have to use malloc() in order to allocate memory for n number of structs during run-time. The structs have to accessed using the base pointer of the memory allocated by malloc(). I also tried typecasting (triangle*)malloc() But that didn't work either. Can anyone tell me how to do this?
My Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
void swapStructs(triangle *a, triangle *b)
{
triangle temp = *a;
*a = *b;
*b = temp;
}
void print_structs(triangle *tr, int n)
{
printf("VALUES BEFORE SWAPPING...\n");
for(int i=0; i<n; i++)
{
printf("A[%d]: %d\n", i ,tr->a);
printf("B[%d]: %d\n", i ,tr->b);
printf("C[%d]: %d\n", i ,tr->c);
tr++;
}
swapStructs(&tr[0], &tr[1]);
printf("VALUES AFTER SWAPPING...\n");
for(int i=0; i<n; i++)
{
printf("A[%d]: %d\n", i ,tr->a);
printf("B[%d]: %d\n", i ,tr->b);
printf("C[%d]: %d\n", i ,tr->c);
tr++;
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++)
{
scanf("%d %d %d", &tr[i].a, &tr[i].b, &tr[i].c);
}
swapStructs(&tr[0], &tr[1]);
print_structs(tr, n);
return 0;
}
Rather than printing the correct output, it printed garbage values.
Just remove the tr++; instructions.
Also the print_structs should be fixed as follow:
void print_structs(triangle *tr, int n)
{
printf("VALUES BEFORE SWAPPING...\n");
for(int i=0; i<n; i++)
{
printf("A[%d]: %d\n", i ,tr[i].a);
printf("B[%d]: %d\n", i ,tr[i].b);
printf("C[%d]: %d\n", i ,tr[i].c);
}
swapStructs(&tr[0], &tr[1]);
printf("VALUES AFTER SWAPPING...\n");
for(int i=0; i<n; i++)
{
printf("A[%d]: %d\n", i ,tr[i].a);
printf("B[%d]: %d\n", i ,tr[i].b);
printf("C[%d]: %d\n", i ,tr[i].c);
}
}

reversing a double array with a recursive function in c without loop in the recursive part of the code

I've been trying to reverse a double array with a recursive function without using loops in the recursive part of the code.
I've written the code for integers and it works for all cases, however, I couldn't reverse the elements of the array as doubles
Is there anyone would like to comment on this at least to give a perception?
Thanks
#include <stdio.h>
#include <stdlib.h>
void ArrayReverse(int x[], int N)
{
if (N <= 1)
{
return;
}
else
{
int temp;
int j = 0;
temp = x[j];
x[j] = x[N-1];
x[N-1] = temp;
ArrayReverse(&x[1],N-2);
}
}
int main(int argc, char *argv[]) {
int num_of_el;
int i;
printf("enter the number of elements of the array: \n");
scanf("%d", &num_of_el);
int arr[num_of_el];
int element;
int k,l;
printf("enter the elements one by one: \n");
for(i=0;i <= num_of_el - 1; i++)
{
scanf("%d", &element);
arr[i] = element;
}
for(l=0;l <= num_of_el - 1; l++)
{
printf("%d", arr[l]);
}
printf("\n");
ArrayReverse(arr,num_of_el);
for(k=0;k <= num_of_el - 1; k++)
{
printf("%d", arr[k]);
}
return 0;
}

Why am I getting segmentation fault and how do I solve this?

C program for bubble sort using a minimum of 4 functions.(input,output,compute,main)
No global variables allowed.
No printf or scanf in compute.
No printf or scanf in main
Input should not call compute.
compute should not call output.
I haven't really understood pointers and functions.
#include <stdio.h>
void input(int* size, int* arr[])
{
printf("Enter the size of the array: ");
scanf("%d",size);
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d", arr[i]);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int arr[100],int size)
{
for(int i = 0;i < size - 1;i++)
{
for(int j = 0;j < size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int size,int* arr)
{
printf("Sorted array\n");
for(int i = 0;i < size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int* input_values[50];
int size;
input(&size, input_values);
bubble_sort(size,*input_values);
output(size, *input_values);
return 0;
}
No errors but showing segmentation fault.How do I solve this?
So your problem is here :
scanf(" %d", arr[i]);
You have to change this to :
scanf(" %d", &arr[i]);
This is the main problem, but there are a lot of others.
Also you have to change the order of parameters in
bubble_sort(size,*input_values);
to
bubble_sort(input_values,size);
and
output(size, *input_values);
to
output(size, input_values);
Also in order this to work at all i have changed the
scanf("%d", &arr[i]);
to
scanf(" %d", &arr[i]);
Actually your code is full of mistakes like the usage of scanf and the usage of pointers and arrays, the following is a workable version of you code see and compare:
#include <stdio.h>
void input(int* size, int arr[])
{
char chr;
printf("Enter the size of the array: ");
scanf( "%d%c", size, &chr );
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d%c", &arr[i], &chr);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int* size,int arr[])
{
for(int i = 0;i < *size - 1;i++)
{
for(int j = 0;j < *size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int* size,int arr[])
{
printf("Sorted array\n");
for(int i = 0;i < *size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int input_values[50];
int s = 0;
int* size = &s;
input(size, input_values);
bubble_sort(size,input_values);
output(size, input_values);
return 0;
}

Using scanf() with a pointer to a double pointer

I feel like I've attempted every combination I know of to get this to work and can't figure it out. How can I scanf() into an int** passed as a pointer to a function? I tried searching but couldn't find this, if it's a duplicate please let me know and I'll delete. It begins to run and after entering a few values it segfaults.
Here's my code, I think it's messing up on the scanf() line of the setMatrix() function:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int ***mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", mat[i][j]); // problem here??
printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
}
}
return;
}
// print matrix
void printMatrix(int ***mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", (*mat)[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(&mat, r, c);
printMatrix(&mat, r, c);
}
There is no need to use triple pointer ***. Passing two-dimensional array will work as is. Here is the code:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int **mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]); // no problem here
printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
}
}
}
// print matrix
void printMatrix(int **mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(mat, r, c);
printMatrix(mat, r, c);
}
Should be:
scanf("%d", &(*mat)[i][j]);
You're passing a pointer to you matrix object, so you need to dereference it (with *) just as you do with printf. scanf then needs the address of the element to write into, so you need the &

argument of type "int *" is incompatible with parameter of type "int (*)[1000]"

i try this code to make a multiplication between matrices its work in my computer but if i try to run it in another its display me an error
argument of type "int " is incompatible with parameter of type "int ()[1000]"
i think that the problem is that i made a dynamic allocation for a normal array but i use it as a 2D array...
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define M 1000
#define N 1000
void randmat (int mat[M][N],int,int);
void printmat (int mat1[M][N],int,int);
void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int,int,int);
void main ()
{
int c1,c2,r1,r2;
int *mat1= (int *)malloc(sizeof(int)*N*M);
int *mat2= (int *)malloc(sizeof(int)*N*M);
int *mat3= (int *)malloc(sizeof(int)*N*M);
printf("enter the Dimensional of the matrix 1:\n");
scanf("%d%d",&r1,&c1);
printf("enter the Dimensional of the matrix 2:\n");
scanf("%d%d",&r2,&c2);
flushall();
while (c1!=r2)
{
printf("worng!!please enter a new Dimensional of the matrix 1:\n");
scanf("%d%d",&c1,&r1);
printf("please enter a new Dimensional of the matrix 2\n");
scanf("%d%d",&c2,&r2);
}
printf("random matrix 1:\n");
randmat(mat1,c1,r1);
printmat(mat1,c1,r1);
printf("random matrix 2:\n");
randmat(mat2,c2,r2);
printmat(mat2,c2,r2);
printf("the multiply of both matrix 1 and 2 :\n");
msklret(mat1,mat2,mat3,r1,c2,r2);
printmat(mat3,c2,r1);
getch();
}
void randmat (int matrix[M][N],int a,int b)
{
int i , j;
for(i=0;i<b;i++)
{
for(j=0;j<a;j++)
{
matrix[i][j]=rand()%90 + 1;
}
}
}
void printmat (int matrix1[][N],int x,int y)
{
int k,l;
for(k=0;k<y;k++)
{
for(l=0;l<x;l++)
printf(" %4d ",matrix1[k][l]);
printf("\n");
}
}
void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int r1,int c2,int r2)
{
int i , j,l,MultiSum = 0;
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
for (l = 0; l < r2; l++)
MultiSum += matrixA[i][l] * matrixB[l][j];
matrixC[i][j] = MultiSum;
MultiSum = 0;
}
}
}
change
int *mat1= (int *)malloc(sizeof(int)*N*M);
int *mat2= (int *)malloc(sizeof(int)*N*M);
int *mat3= (int *)malloc(sizeof(int)*N*M);
to
int (*mat1)[N]= (int (*)[N])malloc(sizeof(int)*N*M);//No need to cast the return value of malloc
int (*mat2)[N]= (int (*)[N])malloc(sizeof(int)*N*M);
int (*mat3)[N]= (int (*)[N])malloc(sizeof(int)*N*M);

Resources