Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program should be in C language: Given a linked list, check whether it is a

ID: 3604705 • Letter: T

Question

This program should be in C language:

Given a linked list, check whether it is an arithmetic progression, geometric progression, or neither Define a function int checkSeries (node head), which takes a linked list as input and checks whether the linked list is an arithmetic progression or geometric progression or neither You will be given N series of integers, each which you have to store in a separate linked list. For each linked list, call the function checkSeries Input Format Take input from a file in3.txt. The first line contains a single integer N, denoting the number of test cases. The first line of each test case contains a single integer L, denoting the length of the series. The second line of each test case contains L integers. Constraints Output Format For each series, print 1 if it is an arithmetic progression, 2 if it is a geometric progression, and 0 if it is neither Sample Input 2 4 6 8 10 10 100 1000 11 12 13 16 Sample Output Explanation There are 3 test cases. The first series contains 5 elements, and is an arithmetic progression. The second series contains 3 elements, and is a geometric progression. The third series contains 4 elements, and is neither

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE

#include<stdio.h>

#include<stdlib.h>

typedef struct Node

{

int value;

struct Node *next;

}node;

node *createNode(node *head);

void add(node *head, int value);

//just written display to print nodes if required

void display(node *head);

int checkSeries(node *head);

int main()

{

int n,i;

node *head = NULL;

FILE *fp;

fp = fopen("series.txt", "r");

if (!fp)

{

printf("not able to open input file ");

return -1;

}

//read number of test cases

fscanf(fp, "%d", &n);

for (i = 0; i < n; i++)

{

//insert into lineked list

int m,j,value ,ret;

node *cur = head,*prev;

//read number of elements

fscanf(fp, "%d", &m);

//insert into linked list

for (j = 0; j < m; j++)

{

fscanf(fp, "%d", &value);

if (head == NULL)

{

//allocate y memory for head

head = createNode(head);

head->value = value;

head->next = NULL;

}

else

add(head, value);

}

//check series

ret = checkSeries(head);

printf("%d ", ret);

//delte previous items

cur = head;

while (cur!= NULL)

{

prev = cur->next;

free(cur);

cur= prev;

}

head = NULL;

}

}

node *createNode(node *head)

{

head = (node*)malloc(sizeof(node));

head->next = NULL;

return head;

}

void add(node *head, int value)

{

node *cur = head;

if (head == NULL)

{

head = createNode(head);

head->value = value;

head->next = NULL;

}

else

{

while (cur->next != NULL)

cur = cur->next;

cur->next = (node*)malloc(sizeof(node));

cur->next->value = value;

cur->next->next = NULL;

}

}

void display(node *head)

{

node *cur = head;

while (cur->next != NULL)

{

printf("%d ", cur->value);

cur = cur->next;

}

printf(" ");

}

int checkSeries(node *head)

{

node *cur = head;

int diff, diff1, ret = 1;

float prod,prod1;

if (cur->next != NULL)

{

diff = cur->next->value - cur->value;

prod = cur->next->value / cur->value;

}

cur = cur->next;

//check for arithmetic progression

while (cur->next != NULL)

{

diff1 = cur->next->value - cur->value;

if (diff1 != diff)

{

ret = 0;

break;

}

cur = cur->next;

}

if (!ret)

{

ret = 2;

//check for geaometric progression

while (cur->next != NULL)

{

prod1 = (float)cur->next->value /cur->value;

if (prod1 != prod)

{

ret = 0;

break;

}

cur = cur->next;

}

}

return ret;

}

/*output

1

2

0

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote