// C program for reading
// struct from a file
#include <stdio.h>
#include <stdlib.h>
// struct person with 3 fields
struct person
{
int id;
char fname[20];
char lname[20];
};
// Driver program
int main ()
{
FILE *infile;
struct person input;
// Open person.dat for reading
infile = fopen ("person.dat", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening
file\n");
exit (1);
}
// read file contents till end of file
while(fread(&input, sizeof(struct person), 1,
infile))
printf ("id = %d name = %s %s\n",
input.id,
input.fname, input.lname);
// close file
fclose (infile);
return 0;
}
2.typedef struct node
{ int id; char foo[255];
}
Node;
int compare(const void * x, const void * y)
{ Node * a = (Node *) x; Node * b = (Node *) y;
return a->id - b->id;
}
int main()
{ Node array[SIZE];
int n; ... qsort(array, n, sizeof(Node), compare); ...
return 0;
}
3.
/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into
arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] =
L[i];
i++;
}
else
{
arr[k] =
R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if
there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if
there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids
overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second
halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver program to test above functions */
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}
5.
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BOOL int #define TRUE 1 #define FALSE 0 static inline char *sgets(size_t buflen, char *buffer) { char *result = fgets(buffer, buflen, stdin); if (result) buffer[strcspn(buffer, "\n")] = '\0'; return result; } #define checkAllocation(x) assert((x) != 0) #define SIZE 100 typedef unsigned char BYTE; char **getStringArr(int *stringSize); int stringBinSearch(char **stringArr, int stringSize, char *stringToFind); int binSearch(void *Arr, int size, int ElemSize, void *Item, int (*compare)(void *, void *)); int compare2Strings(void *str1, void *str2); int main(void) { char **stringArr, stringToFind[SIZE]; int stringSize; int res; stringArr = getStringArr(&stringSize); sgets(sizeof(stringToFind), stringToFind); printf("Strings: %d\n", stringSize); for (int i = 0; i < stringSize; i++) printf("[%d] = [%s]\n", i, stringArr[i]); printf("Search: [%s]\n", stringToFind); res = stringBinSearch(stringArr, stringSize, stringToFind); if (res == 1) printf("The string %s was found\n", stringToFind); else printf("The string %s was not found\n", stringToFind); return 0; } char **getStringArr(int *stringSize) { int i, size, len; char **arr; char temp[SIZE]; scanf("%d", &size); getchar(); arr = (char **)malloc(size * sizeof(char *)); checkAllocation(arr); for (i = 0; i < size; i++) { sgets(sizeof(temp), temp); len = strlen(temp); temp[len] = '\0'; arr[i] = (char *)malloc((len + 1) * sizeof(char)); checkAllocation(arr[i]); strcpy(arr[i], temp); } *stringSize = size; return arr; } int stringBinSearch(char **stringArr, int stringSize, char *stringToFind) { return binSearch(stringArr, stringSize, sizeof(char *), &stringToFind, compare2Strings); } int binSearch(void *Arr, int size, int ElemSize, void *Item, int (*compare)(void *, void *)) { int left = 0, right = size - 1, place; BOOL found = FALSE; while (found == FALSE && left <= right) { place = (left + right) / 2; if (compare(Item, (BYTE *)Arr + place * ElemSize) == 0) found = TRUE; else if (compare(Item, (BYTE *)Arr + place * ElemSize) < 0) right = place - 1; else left = place + 1; } return found; } int compare2Strings(void *str1, void *str2) { char *elemA = *(char **)str1; char *elemB = *(char **)str2; return strcmp(elemA, elemB); }
I know the answers are not int he same format as that demanded in
the question But i am sure it will help to frame it in that way as
the basic coding logic can be studied from the code snippets given
above.
Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...
#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.
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...
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...
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...
C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary: public: Employee; // default constructor: age=0, id=0, and salary=0 void setAge(int x); // let age = x void setId(int x); // let id = x void setSalary(float x); // salary = x int getAge(); // return age int getId; // return id float...
Wondering if I could get some guidance on setting up newMatrix(). Language is C Matrix ADT Specifications In addition to the main program Sparse.c and the altered List.c from pal, you will implement a Matrix ADT in a file called Matrix.c. This ADT will contain a private inner struct (similar to Node in your List ADT) encapsulating the column and value information corresponding to a matrix entry. You can give this inner struct any name you wish, but I will...
4. Complete the code inside the box class dtype int id; float rate; int hours; public: dtype): dtype) void insert(ifstream &); class op dtype s[12): public: op(): op): vold read_into_array(ifstream &); // reads from a file into the array of objects // Write a CoMPLETE code for "read_into_array" method along with the function heading.
c++ please need help with this question Consider a class Employee with data members: age(an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary; public: Employee( ); // default constructor: age=0, id=0, and salary=0 Employee(Employee &x); // copy constructor Employee& operator = (Employee &x); // equal sign operator void setAge(int x); // let age = x...
Using C++, fix the following code so that (1) Change the student array storage part so that the data is loaded from a file "students.txt". Use an appropriate data termination flag. (2) Sort the student array in ascending order of GPA using insertion sort. Print the sorted array (3) Sort the student array in ascending order of ID using insertion sort. Print the sorted array (4) Illustrate Binary search for a particular ID that would be found and one...
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...