Sorting and 2d Arrays – Arithmetic – Problem #2

IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive

Difficulty: medium

Problem Statement

A dynamic array is an array whose size is only known during the runtime of a program. You can use the following two functions, malloc() or calloc() to define dynamic arrays. A strict sawtooth array is an array where the values of the sequence of elements should follow the up-down principle. The successive elements of the array should not be the same. The suc- cessive differences across the array should follow alternate +ve and -ve values. For example (1,4,2,5,4,8,6,10,9) and (-10,-15,0,-2,6,3) are strict sawtooth arrays of size 9 and 6, respectively, but (1,5,7,3,6) and (34,23,23,44) are not strict sawtooth arrays. In this program you have to use the concept of dynamic arrays to perform the following tasks with respect to sawtooth arrays. • Prompt the user to enter the size of the array as ‘n’. Declare a dynamic integer array of the same size. • Take input ‘n’ different integers into the array from the keyboard. • Check if the array is a sawtooth array or not. Use the function int isSawtooth(int *) that takes as argument a pointer to the array and returns 1 if the array is sawtooth, otherwise it returns 0. Print the appropriate message on the console. • If the array is NOT a sawtooth array, then find out the first position in the array where the sawtooth condition is violated. Use the function void position(int *, int *) which takes in two pointer arguments, first one is the pointer to the array and the second one a pointer to another integer variable declared in main. The position where the sawtooth condition is violated is written into the variable pointed by the second argument (Note that this function should NOT return the position). Also this function should be called only if the array is not sawtooth. • The program then frees the dynamic array created. It asks whether the user wants to continue again or not. It prompts the user to input ‘Y’ (for Yes) or ‘N’ (for No) to continue the program. If ‘Y’ is entered, then the steps are repeated again and if ‘N’ is entered, the program exits. The array has to be dynamically allocated each time the program runs. Example: Enter n: 9 Enter the array: 1,4,2,5,4,8,6,10,9 The array is a strict sawtooth array. Do you want to continue (Y/N): Y Enter n: 5 Enter the array: 1,5,7,3,6 The array is not a strict sawtooth array. First violating position: 3 Do you want to continue (Y/N): N

Back to Lab Problems
Sorting and 2d Arrays/Arithmetic/Problem #2

Lab Problem Statement

Specifications & Problem DetailsIIT Kharagpur PDS Archive
Loading question...

Interactive C Compiler · Problem #2

WASM In-Browser

Runs directly on your machine · Zero remote server dependency

Compiler downloads on first Run (large, saved in your browser after that)
main.cCtrl + Enter to run
1
2
3
4
5
6
7
Input (stdin)given to scanf / cin
Output

Output appears here. Press Run to compile and execute.