Anarion's Profile User Rating: -----

Reputation: 332 Architect
Group:
Author w/DIC++
Active Posts:
1,566 (0.7 per day)
Joined:
16-May 09
Profile Views:
37,042
Last Active:
User is offline Today, 11:16 AM
Currently:
Offline

Previous Fields

Country:
IR
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
Playstation
Your Car:
Who Cares
Dream Kudos:
1150

Latest Visitors

Icon   Anarion has not set their status

Posts I've Made

  1. In Topic: Sorting a Randomly populated Array

    Posted 30 Jun 2015

    I would like to note some more things: in your header files, #include <stdlib.h> should be replaced by #include <cstdlib>. You have both of them, remove the former. Also replace #include <time.h> by #include <ctime>. These are to make your code compliant to the standards :)

    It helps you better if you turn the compiler warnings all ("-Wall" in GCC).
  2. In Topic: Sorting a Randomly populated Array

    Posted 30 Jun 2015

    Here, you are redeclaring/defining j:
    int main()
    {
    	int j; //First here...
    	int array[100];
    	srand(time(NULL));
    	for (int j = 0; j < 100; j++) //and then here again!
    	{
    		array[j] = (rand() % 200) + 1;
    		cout << array[j] << endl;
    		
    	}
    

    Note than in a for loop, when you have int j=0;, you are declaring a variable and initializing it to 0; and the scope of this variable is only the loop's scope (that's why you are not receiving a compiler error for that!). Lets illustrate this fact:
    #include <iostream>
    using namespace std;
    
    int main() {
        for(int j=0; j < 100; ++j) {
            //
        }
        
        cout<<"Trying to see what j contains..."<<endl;
        cout<<"j="<<j<<endl;
    
        return 0;
    }
    

    The above code does not even compile!

    Quote

    projects/forum/main.cpp error: ‘j’ was not declared in this scope

    Why? Because j is only visible through the curly braces of the loop. Outside that area, it doesn't exist. Lets see if we can make it visible to main like you did: having two declarations.
    #include <iostream>
    using namespace std;
    
    int main() {
        int j;
    
        for(int j=0; j < 100; ++j) {
            //
        }
        
        cout<<"Trying to see what j contains..."<<endl;
        cout<<"j="<<j<<endl;
    
        return 0;
    }
    

    Now it compiles. The output is:

    Quote

    Trying to see what j contains...
    j=0

    Hmm... now it doesn't complain about j not being declared but why is the value of j zero? Shouldn't it be 100?!
    To explain this behavior, let me tell you that there are two different j variables involved here: one is only visible to the loop (the inner j), while the other is visible to main. The compiler doesn't come up with an error because that's the default behavior: inside the loop, the compiler assumes that whenever you mention j, you mean the one that is only visible to this loop (when there is any). However, when you get outside the loop, that one does not exist anymore! What you end up with is the default-initialized variable and that's why it's value is 0.

    To solve such problem simply have the loop just initialize the variable, not to declare it:
    #include <iostream>
    using namespace std;
    
    int main() {
        int j;
    
        for(j=0; j < 100; ++j) {
            //
        }
    
        cout<<"Trying to see what j contains..."<<endl;
        cout<<"j="<<j<<endl;
    
        return 0;
    }
    

    Now it tells you that "j=100".
  3. In Topic: Help figuring why my program isnt finding average of 5 ints correctly?

    Posted 29 Jun 2015

    By the looks of it, I am guessing the point of this assignment was understanding the difference between int and double/float and casting. After doing this assignment, you should be able to explain why a code like this prints 0 and how to get 0.8 out of it:
    cout<<4/5<<endl;
    


    cout<<(double)4 / 5<<endl;
    


    Also note that for calculating average:

    Quote

    Let set of numbers be 1, 2, 3
    Average then would be: (1 + 2 + 3) / 3 = 2
    If you do the division on the inside of the parenthesis it would become:
    Average: 1/3 + 2/3 + 3/3 = 2


    This way, you can do it just inside the loop, no need to add a division outside of the loop.
    For learning purposes at this stage, there is no need to discuss performance issues and whether to perform division inside a loop or outside of it. Remember that, there might be a reason behind setting limitations :)
    Although I am against those who set limitations on C++ and try to deliberately lead students to the C way of doing things.
  4. In Topic: What's the difference between Arrays & Vectors?

    Posted 29 Jun 2015

    View PostShadow-coder, on 29 June 2015 - 06:45 PM, said:

    Ah, okay so instead of if i had an array with a size of 10 and the user tried to put an 11th element into the array the vector would resize itself and allow that element into the vector? unlike a regular array where it will make your program spaz out or crash.?

    Sorry if i'm not understanding this completely, i've only been programming for about 4 months v,v

    Yes. Read the earlier posts again if you need to, and give it a little time to sink. Arrays do not provide extra functionality for resizing, getting their size or bounds checking. However, std::array is a class. Deep down it contains an array. A vector is a class too, it uses dynamic memory allocation (similar to an array in terms of indexing, but a different memory allocation mechanism).
    All of these classes are written to be re-usable by you and others, so that you don't need to write them on your own and instead focus on what's more important: code design.
  5. In Topic: writing a string into a file...

    Posted 29 Jun 2015

    Don't forget switching to std::string. Trust me, you don't want to use char arrays all over :)

My Information

Member Title:
The Persian Coder
Age:
23 years old
Birthday:
May 17, 1992
Gender:
Interests:
Music, Tennis, Programming, KitKat, Cars, Bikes
Full Name:
Kian Nezhadfard
Years Programming:
5
Programming Languages:
C++, PHP, R, JavaScript, HTML, CSS

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    Hybrid SyntaX Icon

    10 Feb 2014 - 14:02
    sup?
  2. Photo

    estherNamogo Icon

    03 Nov 2013 - 06:33
    Hello
    Nice to meet you write
    me at (esther2namogo@hotmail.com) i will send you my picture OK.
  3. Photo

    jatinjindalj Icon

    06 Sep 2012 - 09:01
    Hey Regarding your code snippet at
    http://www.dreamincode.net/code/snippet4621.htm-->
    Can you please explain:
    why when at line 37 and 38,If I do
    while(q!= NULL) {//Line 37
    q = q->link;
    }
    t = new node;
    t->data = str;
    t->link = NULL;<...
  4. Photo

    Anarion Icon

    07 Aug 2012 - 06:23
    Thanks! I had made it for DIC Signature Contest and put it on my sig since then.
  5. Photo

    AnalyticLunatic Icon

    06 Aug 2012 - 09:05
    Just wanted to drop by and say Sweet Sig. Sometime I need to make one for myself.
  6. Photo

    David W Icon

    07 Jun 2012 - 00:31
    Shalom shalom my Regal Persian Coder Friend
  7. Photo

    Anarion Icon

    16 Mar 2011 - 00:56
    @abrsefid: ba arze sharmandegi, komaki az man sakhte nist. Az beine portha faghat ba USB kar kardam oon ham dar sathe kheili payin. Be shoma pishnahad mikonam baraye javabe soaletun dar internet jostejoo konid :)
  8. Photo

    yogesh201010 Icon

    05 Mar 2011 - 18:41
    hi i am yogesh
    how to connect internet by mob in linux.
    and tell me book for linux learning
    thanks
  9. Photo

    abrsefid Icon

    27 Feb 2011 - 05:23
    salam man donbale 1 code c++ (ubuntu) migardam vase send o receive data ba port serial mishe rahnamaeem konid ?
  10. Photo

    hamishehiran Icon

    10 Aug 2010 - 23:02
    thanks for add! ;) nice to meet you!
  11. Photo

    Anarion Icon

    28 Mar 2010 - 08:27
    Thanks! Happy new year to you too :)
  12. Photo

    saSBH2 Icon

    28 Mar 2010 - 07:03
    Happy New Year :)
Page 1 of 1