Click here to Skip to main content
15,906,455 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
SuggestionRe: New [[nodiscard]] attribute usage. Pin
Maximilien4-Apr-19 16:17
Maximilien4-Apr-19 16:17 
GeneralRe: New [[nodiscard]] attribute usage. Pin
Randor 4-Apr-19 18:53
professional Randor 4-Apr-19 18:53 
GeneralRe: New [[nodiscard]] attribute usage. Pin
leon de boer5-Apr-19 3:24
leon de boer5-Apr-19 3:24 
GeneralRe: New [[nodiscard]] attribute usage. Pin
John R. Shaw5-Apr-19 11:00
John R. Shaw5-Apr-19 11:00 
GeneralRe: New [[nodiscard]] attribute usage. Pin
Rajesh R Subramanian12-Apr-19 0:34
professionalRajesh R Subramanian12-Apr-19 0:34 
Questiontriplet or what ? Pin
Vaclav_31-Mar-19 5:55
Vaclav_31-Mar-19 5:55 
AnswerRe: triplet or what ? Pin
k505431-Mar-19 6:30
mvek505431-Mar-19 6:30 
GeneralRe: triplet or what ? Pin
Vaclav_31-Mar-19 7:27
Vaclav_31-Mar-19 7:27 
GeneralRe: triplet or what ? Pin
k505431-Mar-19 8:38
mvek505431-Mar-19 8:38 
AnswerRe: triplet or what ? Pin
Randor 31-Mar-19 8:34
professional Randor 31-Mar-19 8:34 
GeneralRe: triplet or what ? Pin
k505431-Mar-19 8:45
mvek505431-Mar-19 8:45 
GeneralRe: triplet or what ? Pin
Randor 31-Mar-19 9:04
professional Randor 31-Mar-19 9:04 
GeneralRe: triplet or what ? Pin
Vaclav_31-Mar-19 16:20
Vaclav_31-Mar-19 16:20 
GeneralRe: triplet or what ? Pin
Randor 31-Mar-19 17:21
professional Randor 31-Mar-19 17:21 
GeneralRe: triplet or what ? Pin
leon de boer1-Apr-19 0:49
leon de boer1-Apr-19 0:49 
QuestionCombination or inheritance Pin
元昊 潘31-Mar-19 5:04
元昊 潘31-Mar-19 5:04 
AnswerRe: Combination or inheritance Pin
leon de boer31-Mar-19 6:12
leon de boer31-Mar-19 6:12 
SuggestionRe: Combination or inheritance Pin
David Crow1-Apr-19 3:31
David Crow1-Apr-19 3:31 
Questioni really need help with this Pin
Member 1420332729-Mar-19 5:13
Member 1420332729-Mar-19 5:13 
GeneralRe: i really need help with this Pin
Richard MacCutchan29-Mar-19 5:58
mveRichard MacCutchan29-Mar-19 5:58 
AnswerRe: i really need help with this Pin
Victor Nijegorodov29-Mar-19 6:43
Victor Nijegorodov29-Mar-19 6:43 
GeneralRe: i really need help with this Pin
Member 142033272-Apr-19 4:14
Member 142033272-Apr-19 4:14 
QuestionRe: i really need help with this Pin
David Crow29-Mar-19 9:24
David Crow29-Mar-19 9:24 
AnswerRe: i really need help with this Pin
Member 1420332730-Mar-19 3:52
Member 1420332730-Mar-19 3:52 
GeneralRe: i really need help with this Pin
k505430-Mar-19 4:48
mvek505430-Mar-19 4:48 
Member 14203327 wrote:
int marks [] = {20,30,40,50,60}, a = 0, b = 1 , lowest ;
for (marks[a]<marks[b];b++;){

Let's a look at your for loop. In C the for loop is defined as for( init; condition; increment). Where init is the initial condition of the loop, condition is the end loop condition - that is if the condition expression returns true false, or non-zero, and increment is an expression that defines the next step of the loop. Lets look at your for loop:
init : marks[a] < marks[b] that's effectively a No-Op, it does evaluate to true, but it does not define the starting condition of the loop
condition : b++ I expect you know that in C false is zero and true is any non-zero value. Therefore this expression says return the value of b (= 1), then increment b, so now b = 2. At this point, the end-condition of the loop is 1, or true. that means that that the body of the loop (the portion between { and } never gets executed.. b will continue to be incremented until it gets to INT_MAX (2147483647), at which point it will roll over to INT_MIN (-2147483646) and continue incrementing until it reaches zero. However, once the value of b reaches 5 then the expression lower = marks[a] we get the dreaded undefined behavior, which, if the computer sets itself on fire, is perfectly acceptable, according to the C standard.
increment: empty expression - no action performed. That's not necessarily wrong, there's plenty of times that you might write a for loop without a increment expression. This is not one of those times.

Here's a template for what you should do for your for loop - I'll assume that you do not have C99 or C11, so you need to declare your loop variables outside the for loop:
C
int a; /* index into the marks array */
int lowest = marks[0] /* initialize the lowest variable */
for( /* initial: we already know that lowest is set to marks[0], so maybe we should set our loop variable to the next index in the array */ your code here;
     /* condition: We need to iterate over the entire array, so the condition will be when the loop variable a no longer points to a valid array member.  The array as 5 elements, so the array runs from marks[0] to marks[4]. We'll need to come up with a condition expression that will return true false when a is outside the array bounds */ your code here;
    /* increment: This is where we can change the value of loop variable so the next time through the loop, we check the next element in the array */ your code here) {
   /* If marks[a] is less than lowest, then assign marks[a] to lowest */
   your code here 
}
I hope this helps you understand where you went wrong and how to fix it.

Update: Doh Got the description of the condition 100% completely backwards. Please re-read and note strikeouts, corrections in italics follwing errors

modified 30-Mar-19 13:02pm.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.