Question

-Create a function output() in C that takes the pointer to the array and its size...

-Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format)

(function prototype : void output(int *arrayPtr, int size);

-Create a function check() in C that takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

question 1:

code:

#include <stdio.h>
#include <stdlib.h>

void output(int * arrayPtr, int size){
int i;
for(i = 0; i < size; i++){
printf("%d\n",arrayPtr[i]); //printing all elements
}
}

int main(void) {
int *array;
int size;
  
//for manual input
scanf("%d",&size);
array = (int*)malloc(size * sizeof(int)); //allocating memory
int i;
for(i = 0; i < size; i++){
scanf("%d",&array[i]);
}
  
//if donot want to enter everything mannually then remove comment and comment above code
   // int array[] = {32,9,52,63,78,67,52,34,23,4,36,7,5,78,56,83,43,34,12};
   // int size = sizeof(array)/sizeof(array[0]);

output(array,size);
   return 0;
}

qi.c x q2.c #include <stdio.h> #include <stdlib.h> OWN void output(int * arrayPtr, int size) { int i; for(i = 0; i < size; i+

output:

- O X [/cygdrive/c/Users/rakesh/Desktop/HomeworkLib rakesh@LAPTOP-3KVD9S77 /cygdrive/c/Users/rakesh/Desktop/HomeworkLib $ gcc qi.c rakesh

C/ cygdrive/c/Users/rakesh/Desktop/HomeworkLib - O X rakesh@LAPTOP-3KVD9S77 /cygdrive/c/Users/rakesh/Desktop/HomeworkLib rakesh@LAPTOP-3K

question 2:

code:

#include <stdio.h>
#include <stdlib.h>

int check(int * arrayPtr, int size, int num){
int i;
for(i = 0; i < size; i++){ // traverse whole array
if(arrayPtr[i] == num){ // checking if the element is equal to num
return i; // returning index
break; // stop the loop if num is matched
}
}
return -1; //if num not found return -1
}

int main(void) {
int *array;
int size;
int num;
  
// for manual input
printf("Enter size: ");
scanf("%d",&size);
array = (int*)malloc(size * sizeof(int)); //allocating memory
int i;
printf("Enter elements of array: ");
for(i = 0; i < size; i++){
scanf("%d",&array[i]);
}
  
printf("Enter number to search: ");
   scanf("%d",&num); //for manual input
  
  
//if donot want to enter everything mannually then remove comment and comment above code
   // int array[] = {32,9,52,63,78,67,52,34,23,4,36,7,5,78,56,83,43,34,12};
   // int size = sizeof(array)/sizeof(array[0]);
   // int num = 23; // returns 8 as index start with 0.

   int index = check(array,size,num);
   printf("%d\n",index);
   return 0;
}

1 91.C x q2.c #include <stdio.h> #include <stdlib.h> int check(int * arrayPtr, int size, int num) { int i; for(i = 0; i < sizplhtFl Enter number to search: ); scanf(%d,&num); //for manual input 7/if donot want to enter everything mannually then r

output:

- O X C/cygdrive/c/Users/rakesh/Desktop/HomeworkLib $ subl q2.c rakesh@LAPTOP-3KVD9S77 /cygdrive/c/Users/rakesh/Desktop/HomeworkLib $ gcc

- - o X [/cygdrive/c/Users/rakesh/Desktop/HomeworkLib $ subl q2.c rakesh@LAPTOP-3KVD9577 /cygdrive/c/Users/rakesh/Desktop/HomeworkLib $ g

Add a comment
Know the answer?
Add Answer to:
-Create a function output() in C that takes the pointer to the array and its size...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...

  • Create a function that takes two parameters : pointer to the array and the size of...

    Create a function that takes two parameters : pointer to the array and the size of the array. Print out the array's values and their memory addresses inside the function. Then create another function that takes two parameters : pointer to the array. Inside the function, multiply each of the array's values by two ad print the modified values out in the main function. With C programming language

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Im trying to create a function in C where it takes an array and size and...

    Im trying to create a function in C where it takes an array and size and returns the largest absolute value in the array (either negative or positive). Any help would be greatly appreciated! Ex: if the array is { -2.5, -10.1, 5.2, 7.0}, it should return -10.1 Ex: if the array is {5.1, 2.3, 4.9, 1.0}, it should return 5.1. double getMaxAbsolute(double array[], int size) {     double max = array[0];    double abs[size];    for (int i =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT