Suppose we have a struct type IntPair with members int smaller and int bigger. Implement the function
IntPair getSmallestAndBiggest(int nums[], int length);
that returns an IntPair with the smallest value of nums in member smaller and the biggest value of nums in the member bigger. Note: If length is 1 and more generally if all the elements of nums have the same value then smaller and bigger will be equal. (You may assume that length ≥ 1.)
#include <iostream>
using namespace std;
struct IntPair
{
int smaller;
int bigger;
};
IntPair getSmallestAndBiggest(int nums[], int length);
int main()
{
int nums[] = {5,6,1,2,3,9,8};
IntPair intPair = getSmallestAndBiggest(nums, 7);
cout<<"Smaller: "<<intPair.smaller<<endl;
cout<<"Bigger: "<<intPair.bigger<<endl;
return 0;
}
IntPair getSmallestAndBiggest(int nums[], int length)
{
IntPair intPair;
intPair.smaller = nums[0];
intPair.bigger = nums[0];
for(int i=1;i<length;i++) {
if(intPair.smaller>nums[i]) {
intPair.smaller = nums[i];
}
if(intPair.bigger<nums[i]) {
intPair.bigger = nums[i];
}
}
return intPair;
}
Output:
Smaller: 1
Bigger: 9
Suppose we have a struct type IntPair with members int smaller and int bigger. Implement the...
Assignment Description: We will implement a struct int Container similar to C++’s standard template library std::vector but without
using Object-Oriented Programming. The Container struct will only have member variables and user-defined helper functions that will assist with constructing and destructing instances of the Container type, and to
add items to the Container, check the length of the Container, etc. See below for function prototypes and
descriptions. User-Defined Helper-Functions and Container Member Variables
The helper-functions that operate on the Container data type...
Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...
code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...
Suppose we have three variables declared as char *pc; int *pi; struct point {double x; double y;}; struct point *p1; struct point *p2[10]; Assume sizeof(char)=1, sizeof(int)=4, sizeof(double)=8. The values of pc, pi, p1 and p2 are 240, 258, 410 and 480 respectively. What are the values of pc+1, pi+2, p1+4 and p2+3
Define a type which comprises a struct called
"Maxima". In this struct contains
two int values a
and b. The purpose of this struct is to store
the largest two int values among a set of
integers. The value a is the largest number and
the value b is the second largest number. In order
to accomplish this task, you need to write the following
functions:
allzero( struct pointer ): This function sets
a and b values in a given...
PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT
WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR
IT.
5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...
in C language we need to find the following :
EXERCISE 3: 1. Write the definition of a structure named employee that contains character array members for an employee's first and last names, an int member for the employee's age, a char member that contains 'M' or 'F' for the employee's gender, and a double member for the employee's hourly salary. 2. Using the above mentioned definition, write a C program that asks a user to enter the values of...
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...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...