Question

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 struct to zero.

newnumber( struct pointer , number ): This function gets a number value and introduces it to the set of integers to find the first and second largest values. Namely, it will set a and b properly such that a will be the largest number introduced so far and b will be the second largest. For example, if we consecutively call this function with numbers 1, 4, 5, 2, 10, 3, then a should be equal to 10 and b should be equal to 5.

You are provided main() function for testing purposes. DO NOT MODIFY IT!

} ܒܢܢ 1 #include<stdio.h> 2 #include<string.h> 3 4 // Define struct here 5 typedef //... 6 7 // write allzero(...) function h

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

We will solve this problem using struct datatype and pass by reference mechanism.

  1. pass by reference is a mechanism to modify the data of a variable from an external function.
  2. In most of the cases we are unable to modify variable from external functions so, here we will pass the address of the variable to the function and then we are able to modify the data inside the address from the external functions.
  3. Typedef is a keyword which is used to alias a name, if we say typedef int integer, we can use integer in place of int.
  4. if a is variable &a is the address of variable a

Hope u have understood the prior knowledge lets jump into coding....

SOLUTION

#include<stdio.h>
#include<string.h>
//struct that contains a and b integers
//struct max is typedeg as Maxima.
//
typedef struct max{
int a;
int b;
}Maxima;

//allzero takes parameter as address of struct type and modifies a and b to 0 and 0
void allzero(Maxima *m)
{
m->a=0;
m->b=0;
}

//newnumber is our main task , it takes number and check with both maximum numbers
//if a is less than the number then number is assigned to a and a is assigned to b
//if number is less than a and greater than b then number is assigned to b
//else number is ignored
void newnumber(Maxima *m,int n){
    //we store a value to temp if a is modified then this temp is assigned to b
    int temp=m->a;

    if(n> m->a)
    {
        m->a=n;
        m->b=temp;
    }
    else if(n > m->b)
        m->b=n;

}
//As per your instructions main function is not modified
int main()
{
Maxima m;
allzero(&m);
printf("%d %d\n",m.a,m.b);
newnumber(&m,5);
printf("%d %d\n",m.a,m.b);
newnumber(&m,7);
printf("%d %d\n",m.a,m.b);
newnumber(&m,15);
printf("%d %d\n",m.a,m.b);
newnumber(&m,9);
printf("%d %d\n",m.a,m.b);
newnumber(&m,2);
printf("%d %d\n",m.a,m.b);
return 0;
}

OUTPUT IMAGES ALONG WITH CODE

main.c 1 #include<stdio.h> 2 #include<string.h> 3 // struct that contains a and b integers 4 //struct max is typedeg as Maxim

main.c 25 if(n> m->a) 26 { 27 m->a=n; 28 m->b=temp; 29 30 else if(n > m->b) 31 m->ben; 32 33 } 34 1/As per your instructions

OUTPUT

- 50 7 5 15 7 15 9 15 9 Process returned @ (exe) execution time: 0.130 S Press any key to continue.

Thank you

feel free to post doubts in comment section

please drop a like if u feel it is helpful..

Add a comment
Know the answer?
Add Answer to:
Define a type which comprises a struct called "Maxima". In this struct contains two int values...
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
  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int...

    #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int main(){ struct Employee e; read(&e); } void read(struct Employee *e){ int a,b; printf("Enter the id employee\n"); scanf("%d",&a); printf("Enter the age employee\n"); scanf("%d",&b); e->id=a; e->age=b; } Question: Declare a pointer variable of type Employee and place the address of the variable created in the above problem in that pointer variable.

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • C please In this question, you will find the difference between two time durations. You are...

    C please In this question, you will find the difference between two time durations. You are given the following struct definitions: typedef struct _duration { int hours; int minutes; } Duration; Design a function to find the difference between two durations: Duration* subtract(Duration* duration1, Duration* duration2) { } Return a pointer to a new duration struct that contains the difference between the two. It may be useful to reduce the durations to a single unit (example: minutes), find the difference,...

  • 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...

  • Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO:...

    Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO: 1) Add the typedef here: 5// TODO: 2) Modify the parameter of repeat to add irn the function pointer for the function to be called repeatedly: 8 void repeat (int times) for (int k 0; k < times; ++k) 12 // TODO: 3) Add the call to the function pointer here: 14 15 17 void test (void) 18 printf("Test!\n"); 19...

  • ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){    ...

    ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){       struct node *linked_list = NULL;    linked_list = add_to_list(linked_list, 5, 'a');    linked_list = add_to_list(linked_list, 10, 'b');    linked_list = add_to_list(linked_list, 4, 'c');    linked_list = add_to_list(linked_list, 10, 'd');    linked_list = add_to_list(linked_list, 5, 'e');    linked_list = add_to_list(linked_list, 7, 'f');    linked_list = add_to_list(linked_list, 5, 'g');    linked_list = add_to_list(linked_list, 3, 'h');    int search_number;    printf("Enter number you want to search for:");    scanf("%d", &search_number);    struct node *last_node = find_last(linked_list, search_number);    if (last_node != NULL)    {        printf("Node found: value = %d and...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • 3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

    3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

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