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
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements....
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements. Like Lili, Bibi wants to know the highest frequency (most occurrences) and all elements which have that frequency. Format Input The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤ i ≤...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework 3. The program will create an array of Airport structures. On the first line in the input file airports.txt there is an integer n, representing the number of lines in the input file. On each of the following n lines there is an airport code (a unique identifier) followed by the airport’s number of enplanements (commercial passenger boardings), and city served. You may assume...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT