In C language please, 1 code please. #include <stdio.h> #include <stdlib.h> #inc
ID: 3717973 • Letter: I
Question
In C language please, 1 code please.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct SSL1 {
char text[75];
struct SSL1 *next;
};
//uncomment and complete function1 below
//function1()
//uncomment and complete function2 below
// function2()
Create 2 functions and 1 test case for each function in your main with
the expected output for your functions
listed in comments.
function1() - Linked List - Given a reference to a linked list
consisting of nodes of type SSL1:
(1) print out the number of nodes in the list,
(2) print out the c-strings from the list if the length of the text is
more than 10 characters, and
(3) calculate, print out, and return the number of strings that contain the
same letter as the first letter of your first name
function2() - pointers - Given a pointer to a 1-d array of 50 integers,
do the following without using array notation to access the integers in the array:
(1) print out all of the integers in the array, then
(2) print out only the positive integers from the 1-d array, and
(3) calculate, print out, and return the average of every third integer,
i.e., the average of the integers with indices 0, 3, 6, 9, ...
int main() {
return 0;
}
Explanation / Answer
PLEASE REFER BELOW CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>
typedef struct SSL1
{
char text[75];
struct SSL1 *next;
}SSL1;
int function1(SSL1 **head)
{
SSL1 *temp; //creating pointer for head of linked list
temp = *head; //store head of list
int count;
char initial;
count = 0;
//traversing list and counting nodes of list
while (temp != 0)
{
count++;
temp = temp->next;
}
printf(" Number of nodes in list = %d ", count);
temp = *head;
printf(" print out the c-strings from the list if the length of the text is more than 10 character ");
while (temp != 0)
{
if(strlen(temp->text) > 10)
printf("%s ", temp->text);
temp = temp->next;
}
printf(" calculate, print out, and return the number of strings that contain the same letter as the first letter of your first name ");
fflush(stdin);
printf(" Enter First letter of your first name : ");
scanf("%c", &initial);
temp = *head;
count = 0;
while (temp != 0)
{
if(temp->text[0] == initial)
{
printf("%s ", temp->text);
count++;
}
temp = temp->next;
}
return count;
}
float function2(int *arr)
{
int i = 0;
float avg;
printf(" All elements of array ");
for(i = 0; i < 50; i++)
{
printf("%d ", *(arr+i)); //printing elements of array without using array notation
}
printf(" Positive Integers of array ");
for(i = 0; i < 50; i++)
{
if(*(arr+i) > 0)
printf("%d ", *(arr+i));
}
for(i = 0; i < 50; i+=3)
{
avg += *(arr+i);
}
return(avg / (i/3)) ;
}
int main()
{
SSL1 *head,*first,*temp=0;
int arr[50];
int i;
int choice,count_string;
float average;
printf("Creating Linked list..... ");
first = 0;
choice=1;
while(choice)
{
head = (SSL1*)malloc(sizeof(SSL1));
printf("Enter Data : ");
scanf("%s", head->text);
if (first != 0)
{
temp->next = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)? ");
scanf("%d", &choice);
}
temp->next = 0;
head = first;
count_string = function1(&head);
printf("number of strings that contain the same letter as the first letter of your first name = %d ", count_string);
srand(time(NULL));
for(i = 0; i < 50; i++)
{
arr[i] = rand();
}
average = function2(arr);
printf(" The average of every third integer = %f ", average);
return 0;
}
PLEASE REFER BELOW OUTPUT
Creating Linked list.....
Enter Data : AdamSan
Do you want to continue(Type 0 or 1)?
1
Enter Data : RickeyMartin
Do you want to continue(Type 0 or 1)?
1
Enter Data : BretLee
Do you want to continue(Type 0 or 1)?
1
Enter Data : BrianLara
Do you want to continue(Type 0 or 1)?
1
Enter Data : Chrisgayle
Do you want to continue(Type 0 or 1)?
1
Enter Data : SachinTendulkar
Do you want to continue(Type 0 or 1)?
0
Number of nodes in list = 6
print out the c-strings from the list if the length of the text is more than 10 character
RickeyMartin
SachinTendulkar
calculate, print out, and return the number of strings that contain the same letter as the first letter of your first name
Enter First letter of your first name : K
number of strings that contain the same letter as the first letter of your first name = 0
All elements of array
31449 10345 12241 7 22537 16963 2116 21986 17686 2691 28562 5395 2161 7397 1359
18029 4339 20718 5826 18087 15045 31544 10988 12885 23421 22795 4037 19991 22921 23084
19657 16145 21201 15610 7859 10139 5784 12470 24713 31801 30371 29219 1496 9421 13214
5711 2625 15883 2235 30640
Positive Integers of array
31449 10345 12241 7 22537 16963 2116 21986 17686 2691 28562 5395 2161 7397 1359
18029 4339 20718 5826 18087 15045 31544 10988 12885 23421 22795 4037 19991 22921 23084
19657 16145 21201 15610 7859 10139 5784 12470 24713 31801 30371 29219 1496 9421 13214
5711 2625 15883 2235 30640
The average of every third integer = 12913.470703
Process returned 0 (0x0) execution time : 46.028 s
Press any key to continue.