#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int* ptr1;
int n; int i;int sum = 0;
n = 5;
printf("Number of elements: %d", n);
ptr = (int*)malloc(n * sizeof(int));
ptr1 = (int*)calloc(n, sizeof(int));
if (ptr == NULL || ptr1 == NULL)
{
printf("\nMemory not allocated.");
}
else
{
printf("\n\nMemory successfully allocated using malloc.");
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
printf("\nThe elements of the first array (using malloc)
are: ");
for (i = 0; i < n; i++)
{
printf("%d ", ptr[i]);
}
printf("\nThe elements of the second array (using calloc")
are: ");
//showing that calloc introduces with 0
for (i = 0; i < n;i++)
{
printf("%d ", ptr1[i]);
}
//section 2
n = 12;
printf("\n\nnumber of elements in the first pointer : %d",
n);
//reallocation usage
int *ptr_new;
ptr_new =(int *)realloc(ptr,sizeof(int)*n ); //observe how
it works like calloc
printf("\nMemory successfully re-allocated using realloc.");
for (i = 5; i < 10;i++)
{
ptr_new[i] = i + 1;
}
printf("\nThe elements of the first array are: ");
for (i = 0; i < 10; i++)
{
printf("%d, ", ptr_new[i]);
}
//usage of free, literally frees up the memory assigned to the pointer
free(ptr_new);
printf("\n\nMalloc Memory successfully freed.");
free(ptr1);
printf("\n\nCalloc Memory successfully freed.");
}
return 0;
}
Introduction: When preparing for a data science interview, brushing up on your coding and statistical knowledge is crucial—but math puzzles also play a significant role. Many interviewers use puzzles to assess how candidates approach complex problems, test their logical reasoning, and gauge their problem-solving efficiency. These puzzles are often designed to test not only your knowledge of math but also your ability to think critically and creatively. Here, we've compiled 20 challenging yet exciting math puzzles to help you prepare for data science interviews. We’ll walk you through each puzzle, followed by an explanation of the solution. 1. The Missing Dollar Puzzle Puzzle: Three friends check into a hotel room that costs $30. They each contribute $10. Later, the hotel realizes there was an error and the room actually costs $25. The hotel gives $5 back to the bellboy to return to the friends, but the bellboy, being dishonest, pockets $2 and gives $1 back to each friend. No...
Comments
Post a Comment