0

Please help me what's wrong with my code I have been trying to develop

Hi, I have been trying to develop a program which checks if a student is regular or not, I'm unable to understand what is wrong with the code because I feel the logic is right(CODE IS ATTACHED TO THIS THREAD) I have created: 3 arrays of 55 elements as 3 classes's attendence, A integer variable 'c' for determining the total number of classes, Another, integer array for flag 3 for loops checking arrays which increments the flag value by 1 if number is found in the array(attendence list) one for loop to check ch array values comparing with the number of classes then printing if the student is regular or not. https://code.sololearn.com/ctqriAjOBPYQ/?ref=app

23rd Apr 2021, 5:22 PM
Sreenesh
Sreenesh - avatar
6 Antworten
+ 2
You are comparing first Element of array with 0 , Compare like this 👇 for(int i=0;i<55;i++) {if(arr[i]==i+1) //which 1==1(first Element)and so on {....} }
23rd Apr 2021, 5:33 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 2
On line 15, you are doing `ch[i] = ch[i] + 1` But remember that the elements of `ch` are uninitialized, meaning that they are not set to 0 by default. Sp you end up having garbage values To fix this, change line 11 to `int ch[55] = {0};` On line 33, you are starting i from 1, which means you are skipping ch[0] (the attendence of 1). So the output for 2 is actuallty the output for 3, the output for 3 is actually the output for 4 and so on. This one is a logic error. The conditions on line 14, 19 and 24 are bad. Take the example of 3. 3 appears in all three classes, but the output for 3 is still "somewhat regular". This is because on line 24, you are checking `a3[i] == i + 1` When i=2 `a3[2] == 3` Clearly, this is false, and hence ch[i] is not incremented. If it is guaranteed that the vales of the arrays a, a2 and a3 will be in ascending order, then you can use the bsearch() function of the <stdlib.h> header to check if a number exists in an array.
23rd Apr 2021, 8:02 PM
XXX
XXX - avatar
+ 2
Fixed code (make sure to read the comments) https://code.sololearn.com/cNsui6oXe4Ma/?ref=app
23rd Apr 2021, 8:03 PM
XXX
XXX - avatar
+ 1
Hey Sreenesh really I don't understand your code . but some are the mistake I found i will tell you 1. Your ch[55] array is updating with three loops fisrt it contains a[ ] , then a2 [ ] like that 2. No meaning of line 29 to 31
23rd Apr 2021, 5:56 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Hi, 13. Thank you for solving a part of the problem, Now the program isn't printing "everyone" who is regular, irregular and somewhat regular. Idk why is that happening also pls help me out, I would appreciate that.
23rd Apr 2021, 5:38 PM
Sreenesh
Sreenesh - avatar
0
Thank You 13 and XXX for solving the problem, I understood what I did wrong,
24th Apr 2021, 3:25 AM
Sreenesh
Sreenesh - avatar