Click here to Skip to main content
15,923,015 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: While loop not working on C. Pin
Tarun Jha8-Oct-17 6:24
Tarun Jha8-Oct-17 6:24 
GeneralRe: While loop not working on C. Pin
geodoom12-Oct-17 12:12
geodoom12-Oct-17 12:12 
GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 5:40
Tarun Jha13-Oct-17 5:40 
AnswerRe: While loop not working on C. Pin
«_Superman_»8-Oct-17 17:59
professional«_Superman_»8-Oct-17 17:59 
GeneralRe: While loop not working on C. Pin
Tarun Jha10-Oct-17 5:24
Tarun Jha10-Oct-17 5:24 
AnswerRe: While loop not working on C. Pin
Tarun Jha11-Oct-17 7:15
Tarun Jha11-Oct-17 7:15 
SuggestionRe: While loop not working on C. Pin
David Crow11-Oct-17 7:46
David Crow11-Oct-17 7:46 
GeneralRe: While loop not working on C. Pin
geodoom13-Oct-17 7:24
geodoom13-Oct-17 7:24 
Since you present a working solution you deserve to receive some more elegant alternatives.
Here is how I improved your code , there are some nice tricks for you to learn from it

C++
#include <stdio.h>
#define SIZE 15
int main()
{
    int num[SIZE], i=0 , max;
    int count = 0;  // be sure that we do not read more than 15 elements
    printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
    /*
    scanf(" %d", &num[i]);
    while (num[i]!=0 && count < SIZE )
    {
        ++count;
        scanf(" %d", &num[++i]);
    }
    */  
    /* Either the above while loop */
    do{
        scanf(" %d", &num[i]);
        count++;
      } while(num[i++]!=0 && count < SIZE);
    /* Or the above do { } while loop */
    max = num[0];   /* Avoid to altert any of your array elements */
    for(i=1; i< count; i++)
        if(max < num[i]) max = num[i];  /* each time we find a better max, update our max value */
    printf("%d is the greatest of them all !\n", max);
    return 0;
}


I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE !
Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].

modified 13-Oct-17 13:39pm.

GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 7:26
Tarun Jha13-Oct-17 7:26 
AnswerRe: While loop not working on C. Pin
Manish K. Agarwal12-Oct-17 4:33
Manish K. Agarwal12-Oct-17 4:33 
GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 5:46
Tarun Jha13-Oct-17 5:46 
GeneralRe: While loop not working on C. Pin
David Crow13-Oct-17 16:13
David Crow13-Oct-17 16:13 
AnswerRe: While loop not working on C. Pin
asa7612-Oct-17 6:33
asa7612-Oct-17 6:33 
QuestionRe: While loop not working on C. Pin
David Crow13-Oct-17 2:40
David Crow13-Oct-17 2:40 
QuestionCTreeObject class Pin
_Flaviu3-Oct-17 22:59
_Flaviu3-Oct-17 22:59 
AnswerRe: CTreeObject class Pin
«_Superman_»3-Oct-17 23:07
professional«_Superman_»3-Oct-17 23:07 
GeneralRe: CTreeObject class Pin
_Flaviu5-Oct-17 21:38
_Flaviu5-Oct-17 21:38 
QuestionCImage processing Pin
rbrunton3-Oct-17 2:03
rbrunton3-Oct-17 2:03 
GeneralRe: CImage processing Pin
Richard MacCutchan3-Oct-17 7:53
mveRichard MacCutchan3-Oct-17 7:53 
Questionraytracer Pin
bluatigro29-Sep-17 3:36
bluatigro29-Sep-17 3:36 
QuestionRe: raytracer Pin
David Crow29-Sep-17 4:21
David Crow29-Sep-17 4:21 
AnswerRe: raytracer Pin
Jochen Arndt29-Sep-17 4:24
professionalJochen Arndt29-Sep-17 4:24 
GeneralRe: raytracer Pin
leon de boer30-Sep-17 7:50
leon de boer30-Sep-17 7:50 
GeneralRe: raytracer Pin
Jochen Arndt30-Sep-17 22:14
professionalJochen Arndt30-Sep-17 22:14 
AnswerRe: raytracer Pin
Rick York29-Sep-17 4:44
mveRick York29-Sep-17 4:44 

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.