+ 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.
22 Réponses
+ 16
Make an array and use for loop to takes input to array.
Use loop to get the value of the array.😊
+ 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
+ 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?
+ 2
@Sayan I don't understand, how can we know which number appears frequently if we don't have frequency statistics?
+ 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!
0
@Nithiwat read description. I want output as element that appear 3 or more times
0
but what if 0 is an element?
0
it will mess up that thing. that's where I am stuck. I need a way to delete that element.
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.
0
I know that can be done with string. but the question specifies 1D array not string
0
is there null in array?
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
0
@Shawn Daley I don't want it in structure and all. simple with loops
- 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
- 1
then take the numbers as strings..
put any alphabet string instead of 0
- 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 }
- 1
no need fr 2D array..
see i posted...last post
- 1
i told u
in array put numbers..
when its same...
flag+=1
replace with null
check for if not null if same
- 1
2 for loop...
i 0 t0 18
j i+1 to 19
- 1
ya...
ya just do as
1st for loop executes only when
arr[i] != null
for next loop
arr[j] != null