Run your code, submit the output and code too. Submit code in a separate, compli
ID: 3723428 • Letter: R
Question
Run your code, submit the output and code too. Submit code in a separate, compliable file, do NOT include it in your pdf or text file. Write a structure with 2 fields: a1 and 11, where the former is an int pointer and the latter is an int. The name of the structure is arr. Conceptually, we can consider each instance of arr as an int array of length /1 [1 point]. 1. 2. Write a function called sum that takes as input an arr1 argument, and returns the sunm of all elements in the input array. Test your code in a main function [1 point] Write a function called getOdd that takes as input an arr1 argument, and returns the elements in arr1 that are odd. The output should be of type arr. Test your code in a main function [1 point]. Do you need dynamic memory allocation in getOdd [1 point]? 3. Like always: Run your code, submit the output and code too. Submit code in a separate, compliable file, do NOT include the code in your pdf or tex file. Submit only 1 compressed (gz.tar or .zip NOT rar) file for this HWExplanation / Answer
#include<stdlib.h>
#include<stdio.h>
// Structure arr definition
struct arr
{
// To store data
int a1;
// To store link
struct arr *l1;
};// End of structure
// Start pointer
struct arr *start = NULL;
// Function to create a node
void create()
{
// Temporary pointer of arr declared
struct arr *newNode, *ptr;
// Allocates memory dynamically
newNode = (struct arr *)malloc(sizeof(struct arr));
// Checks if temp is null memory allocated error message displayed
if(newNode == NULL)
{
printf(" Error: Out of Memory Space: ");
exit(0);
}// End of if condition
// Accepts data
printf(" Enter the data value for the node: ");
scanf("%d", &newNode -> a1);
// Sets the next pointer to null
newNode -> l1 = NULL;
// Checks if the start is null then this is the first node
if(start == NULL)
// Sets the new node to start
start = newNode;
// Otherwise at least one node is available
else
{
// Set the temporary pointer to start
ptr = start;
// Loops till end by pointing to next node and checks if it is null
while(ptr -> l1 != NULL)
// Move to next node
ptr = ptr -> l1;
// temporary node next pointing to new node
ptr -> l1 = newNode;
}// End of else
}// End of create() function
// Function to display the list
void display()
{
// Creates a temporary node
struct arr *ptr;
// Checks if start is null then display error message
if(start == NULL)
{
printf(" Error: List is empty: ");
return;
}// End of if condition
// Otherwise at least one nod available
else
{
// Temporary pointer points to start
ptr = start;
printf(" The List elements are: ");
// Loops till end of the list
while(ptr != NULL)
{
// Displays the data
printf("%d ",ptr -> a1);
// Move to next node
ptr = ptr->l1;
}// End of while
}// End of else
}// End of display() function
// Function to calculate sum and return it
int sum(struct arr *arr1)
{
// Creates a temporary node
struct arr *ptr;
// Assigns total to zero
int total = 0;
// Checks if start is null then display error message
if(start == NULL)
{
printf(" Error: List is empty: ");
return;
}// End of if condition
// Otherwise at least one nod available
else
{
// Temporary pointer points to start
ptr = start;
// Loops till end of the list
while(ptr != NULL)
{
// Calculates total
total += ptr->a1;
// Move to next node
ptr = ptr->l1;
}// End of while
}// End of else
// Returns the total
return total;
}// End of sum() function
// Function to display odd numbers from the list
void getOdd(struct arr *arr1)
{
// Declares a temporary node
struct arr *ptr;
// Checks if start is null then display error message
if(start == NULL)
{
printf(" Error: List is empty: ");
return;
}// End of if condition
// Otherwise at least one nod available
else
{
// Temporary pointer points to start
ptr = start;
printf(" Odd Numbers: ");
// Loops till end of the list
while(ptr != NULL)
{
// Checks for odd number
if(ptr->a1 % 2 != 0)
// Displays the number
printf("%5d", ptr->a1);
// Move to next node
ptr = ptr->l1;
}// End of while
}// End of else
}// End of getOdd() function
// main function definition
int main()
{
int x;
// Loops 10 times
for(x = 0; x < 10; x++)
// Calls the function to create 10 nodes
create();
// Calls the function to display all nodes
display();
// Calls the function to calculate sum and display it
printf(" sum = %d", sum(start));
// Calls the function to display odd numbers in the list
getOdd(start);
}// End of main function
Sample Output:
Enter the data value for the node: 10
Enter the data value for the node: 2
Enter the data value for the node: 5
Enter the data value for the node: 3
Enter the data value for the node: 66
Enter the data value for the node: 7
Enter the data value for the node: 9
Enter the data value for the node: 18
Enter the data value for the node: 17
Enter the data value for the node: 33
The List elements are:
10 2 5 3 66 7 9 18 17 33
sum = 170
Odd Numbers: 5 3 7 9 17 33
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.