Question

In: Computer Science

*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...

*C PROGRAMMING LANGUAGE*

a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55}

b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at index 2 should be 1.0, and so on. your function must be called in the main. Also, declaring array and the size must be using heap.

Solutions

Expert Solution

SOLUTION A sorting using pointers:

#include<stdio.h>
#include<stdlib.h>
int main(){
   int n,i,temp,j;
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);
   int *a;
   a=(int*)malloc(sizeof(int)*n);
   if(a==NULL){
       printf("Memory is not allocated\n");
       return 0;
   }
   printf("Enter the elements in array\n");
   for(i=0;i<n;i++){
       scanf("%d",a+i);//a+i is address
   }
   //sorting logic
   for(i=0;i<n;i++){
       for(j=0;j<n;j++){
           if(*(a+i)<*(a+j)){
               //swap a[i] and a[j]
               temp=*(a+i);
               *(a+i)=*(a+j);
               *(a+j)=temp;
           }
       }
   }
   printf("The sorted array is :\n");
   for(i=0;i<n;i++){
       printf("%d ",*(a+i));
   }
   return 0;
}

SCREENSHOT OF THE CODE:

OUTPUT OF THE CODE:

SOLUTION B:

#include<stdio.h>
#include<stdlib.h>
void function(double *a,int n){
   double j=0,k=2;
   int i;
   for(i=0;i<n;i++){
       //double/double is always double only
       a[i]=j/k; //values in the array are half
   j++;
   }
   for(i=0;i<n;i++){
       printf("%lf ",a[i]);
   }
}
int main(){
double *a;
//declares array of 256 numbers
a=(double*)malloc(sizeof(double)*256);

function(a,256);//calling function

}

SCREENSHOT OF THE CODE:

SCREENSHOT OF THE OUTPUT:

NOTE:

If you are satisfied with my answer please do upvote and if you have any kind of doubts please post in the comment section. I'll surely help you there.
Thank You:)


Related Solutions

Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Using C language: Write a program that asks the user for the size of an array,...
Using C language: Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT