+ 1

How to make this C program?

A C program to take 20 element array as input and print elements that appear 3 or more times. Each number must appear only once.

9th Sep 2017, 2:15 AM
Shantanu Shinde
Shantanu Shinde - avatar
22 ответов
+ 16
Make an array and use for loop to takes input to array. Use loop to get the value of the array.😊
9th Sep 2017, 2:36 AM
Nithiwat
Nithiwat - avatar
+ 2
Create 2 dimension array (20x2). First dimension stores the number, second dimension stores number appearance frequency. Make a function (returns void), that accepts a number, and an array (number storage), the function searches for the number in the array, and, if the number already exist, increment its frequency counter, or add the number to the array with 1 as frequency. If the function found that a number frequency equals to three (3) it must print the number before incrementing number frequency. This will ensure that a frequent number is only printed once, that is when its frequency equals to 3, before the number frequency is incremented. In the main function: Get user input twenty times, on each input, call the function passing the number and the array. NOTE: You don't need to call the function for the first input, because the array is empty then, instead directly assign the number into the array with 1 as its frequency. Hth, cmiiw
9th Sep 2017, 3:32 AM
Ipang
+ 2
@Shantanu Sinde, read my post on the 3rd paragraph, I already said, a number is printed once, when it is equal to 3, not after it's > 3, or if it's < 3 if(array[x][1] == 3) { //print the number //increment frequency } else { //increment frequency } A 1D array can only hold the numbers, where can we store the frequency?
9th Sep 2017, 4:06 AM
Ipang
+ 2
@Sayan I don't understand, how can we know which number appears frequently if we don't have frequency statistics?
9th Sep 2017, 4:12 AM
Ipang
+ 2
@Shantanu, did you read my post? the function checks for a number, and updates its frequency, with optionally printing the number under a condition (frequency = 3). I never proposed to ask the user for the frequency!
9th Sep 2017, 4:24 AM
Ipang
0
@Nithiwat read description. I want output as element that appear 3 or more times
9th Sep 2017, 3:01 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
but what if 0 is an element?
9th Sep 2017, 3:20 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
it will mess up that thing. that's where I am stuck. I need a way to delete that element.
9th Sep 2017, 3:21 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
but the frequency will still be increased after print. so when that element comes again, it will be printed. and question specifies to take input as 1D array.
9th Sep 2017, 3:47 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
I know that can be done with string. but the question specifies 1D array not string
9th Sep 2017, 3:49 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
is there null in array?
9th Sep 2017, 4:00 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
you cannot take frequency as input from user. if we create 2d array it will take input for frequency as well in the for loop
9th Sep 2017, 4:19 AM
Shantanu Shinde
Shantanu Shinde - avatar
0
@Shawn Daley I don't want it in structure and all. simple with loops
9th Sep 2017, 4:28 AM
Shantanu Shinde
Shantanu Shinde - avatar
- 1
declair array[20] scan elements take flag as 0 search from 1 st to last element in for loop if a element is 3 times or above flag will be incremented to 3 or above.. now whenever u find a match replace that with 0 suppose so u will never came accross the same number twice.. print numbers with flag>=3
9th Sep 2017, 3:10 AM
sayan chandra
sayan chandra - avatar
- 1
then take the numbers as strings.. put any alphabet string instead of 0
9th Sep 2017, 3:22 AM
sayan chandra
sayan chandra - avatar
- 1
or do same as before... put null in there null means ' \0 ' then in the if statement put a condition if arr[i] == ' \ 0 ' { continue }
9th Sep 2017, 3:24 AM
sayan chandra
sayan chandra - avatar
- 1
no need fr 2D array.. see i posted...last post
9th Sep 2017, 3:48 AM
sayan chandra
sayan chandra - avatar
- 1
i told u in array put numbers.. when its same... flag+=1 replace with null check for if not null if same
9th Sep 2017, 3:51 AM
sayan chandra
sayan chandra - avatar
- 1
2 for loop... i 0 t0 18 j i+1 to 19
9th Sep 2017, 3:52 AM
sayan chandra
sayan chandra - avatar
- 1
ya... ya just do as 1st for loop executes only when arr[i] != null for next loop arr[j] != null
9th Sep 2017, 4:02 AM
sayan chandra
sayan chandra - avatar