Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
G

geodoom

@geodoom
About
Posts
4
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • While loop not working on C.
    G geodoom

    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

    #include
    #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 ].

    C / C++ / MFC data-structures

  • While loop not working on C.
    G geodoom

    First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable). Second suspicious point: you are to hasty to increment i. If you want to insist using a

    do {
    ...
    } while (condition)

    loop , and not a

    while (condition)
    { ... }

    then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element. Try to implement those 2 points , and also as an extra exercise , implement the while(condition) { ... } It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your do { ... } while(condition)

    C / C++ / MFC data-structures

  • The C++ primer 5th Edition (by Lippman ), an example of bad way to express something.
    G geodoom

    So I have started reading the book , and on chapter 6 Functions, 6.3 Return Types and the return statement, he says : "Return from main There is one exception to the rule that a function with a return type other than void must return a value: The main function is allowed to terminate without a return. If control reaches the end of main and there is no return, then the compiler implicitly inserts a return of 0". And I am thinking : What? Why mr. Lippman ? why do you have to express a simple idea in such a complicated way , and mention something that is on the edge of inaccuracy ? You should say : There is NO exception to the rule that a function with a return type other than void must return a value. But because we , the programmers , do not like to type things when not necessary , the compiler lets us omit typing explicitly the return 0; in function main.So instead of

    int main() {
    //some code here
    return 0;
    }

    you are allowed to type

    int main() {
    //your code
    }

    and the compiler will happily insert the implicit return 0; so main() returns a value as it should. I know it is slightly longer as text , but it is what you should mention. I have many other examples from this book on which I could complain. Generally it is a correct book , and anyone who wants to learn C++ cannot avoid it, but although the information is there (and it is correct) it always fails to present that information in a way that will help you to remember it , or fails to aid in categorizing and summarizing the new information. I constantly feel that the only way for this book to be useful is to use it as a primary trustful source for writing my own notes (and they would be as many pages as the book !). But anyway , I am doing all the exercises , and keep reading. I plan to read on parallel the book from I.Horton, Beginning C++ (2015 edition) to get some coverage for C++14.

    The Weird and The Wonderful c++ help tutorial question learning

  • Learning C++
    G geodoom

    Have you been programming in any other languages all these years from college? Generally , the book "Thinking in C++" from Eckel has good reviews in amazon, and you can find it free on line from the authors website. I have not read it yet but it seems a good and free choice. I have read most of Lafores book of 2003 edition , and also I have read most of the book "A complete guide to programming in C++" by Prinz, from 2001. I found them both good introductory books. But if you have been programming in some language all these years , then maybe you could use "Professional C++ , 3rd edition" , a book of 2014 , so you will be current with C++14. And since you are not a novice , the new edition of Lipman's book "C++ primer , 2012 edition" would still be OK for you , I do not think you will have a hard time to follow it, although it may be a little boring.

    The Lounge
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups