Thread: calculating square of some numbers

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    calculating square of some numbers

    Hello ,

    As a exercise I must calculate the square of the number 1 till 100.

    I have this so far:

    main.cpp
    Code:
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "square.h"
    
    
    using namespace std;
    
    int main()
    {
    	vector<int> nummer;
    	int teller ;
    
    
        // filling the vector with the numbers 1 till 100 
    	for (teller=0; teller < 100; teller++ )
    	{
    	    nummer.push_back(teller);
    	}
    
    
    	// compute and write the square
    		for (teller=0 ; teller < 100 ; teller++)
    		try {
    			double uitkomst  = wortel (nummer[teller]); // function where the square is computed.
    			streamsize prec = cout.precision();
    			cout << setprecision(3) << uitkomst
    			     << setprecision(prec);
    		} catch (domain_error e) {
    			cout << e.what();
    		}
    		cout << endl;
    	}
    	return 0;
    }
    And square.h looks like this :
    Code:
    #include <stdexcept>
    #include <vector>
    #include <math>
    
    using namespace std;
    
    
    // compute the square
    double wortel (int nummer)
    {
    	uitkomst = sqrt (nummer) ; 
    	return uitkomst
    }
    But when compiling I get this error message :
    C:\Users\wobben\Desktop\borland-source\chapter04\wortel\square.h|10|error: 'square' was not declared in this scope|

    Im confused because I only use sqaure as name of the header file.

    So where did I go wrong ?

    Roelof

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The first error that I get from the header: "math - no such file or directory".

    The header is called "cmath", and it is the only standard header that you need to include for "square.h" as you have shown.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    You don't really need to bother with vectors for this exercise. You can simply use a loop:

    Code:
    for(int i = 1; i <=100; ++i) {
    cout << i*i << endl;
    }
    That will output all squares without storing the data.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    But I have to use setw for the output.
    Therefore I use vectors.

    Roelof

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Oke,

    But I have to use setw for the output.
    Therefore I use vectors.

    Roelof
    You can also use setw when using cout. You will probably want it left justified so I think it would be cout << setw(n) << left << i*i << endl;

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    But then why this exercise in a chapter full of vectors and structs.
    Then your solution does not seem logical for me.

    Roelof

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why is difficult to say. Suffice to say that they did it for a reason. But you don't have to do it to complete the exercise.
    Also, note that you shouldn't use "using namespace std" inside headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you wanted to compute the sum of a simple series, like 1 + 2 + 3 + 4 + 5, there is no need to store those values in a vector first.

    Code:
    int sum = 0;
    for (int i = 1; i <= 5; ++i) {
        sum += i;
    }
    (Of course, this could be done mathematically without any loop at all.)

    Also, I'm not at all sure, whether there is anything in the try block that can actually throw a domain_error. (It would make sense to me that the sqrt function, coming from a C library, doesn't use methods of error handling that are specific to C++ only.)

    Your wortel() function has other errors. It is using an undeclared variable (uitkomst) and missing a semicolon.

    The main code has mismatching braces.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I think that the meaning of this exercise is a practise of using header files.
    I solved most of the errors , but I still have one.

    code main.cpp
    Code:
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "square.h"
    
    
    using namespace std;
    
    int main()
    {
    	vector<int> nummer;
    	int teller ;
    
    
        // filling the vector with the numbers 1 till 100
    	for (teller=0; teller < 100; teller++ )
    	{
    	    nummer.push_back(teller);
    	}
    
    
    	// compute and write the square
    		for (teller=0 ; teller < 100 ; teller++)
    		{
    		   double uitkomst  = wortel (nummer[teller]); // function where the square is computed.
    			streamsize prec = cout.precision();
    			cout << setprecision(3) << uitkomst
    			     << setprecision(prec);
    		}
    		cout << endl;
    	return 0;
    }
    code square.h
    Code:
    #ifndef GUARD_square_h
    #define GUARD_square_h
    
    // `grade.h'
    #include <vector>
    
    int wortel(int);
    
    #endif
    and as last square.cpp
    Code:
    #include <vector>
    #include "square.h"
    
    // compute a student's overall grade from midterm and final exam grades and homework grade
    int wortel (int nummer)
    {
    	return sqrt(nummer);
    }
    The only error I get now is :
    C:\Users\wobben\Desktop\borland-source\chapter04\wortel\main.o:main.cpp|| undefined reference to `wortel(int)'|

    What wrong here ?

    Roelof

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First,
    Code:
    int wortel(int);
    should be
    Code:
    int wortel(int number);
    Secondly, it seems you aren't compiling properly. You must compile all the source files, not just main.cpp.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I changed that in square.h and if I now compiling square.cpp I get this message :
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|

    on main.cpp I get still the old error message

    Roelof

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How are you compiling?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Im first try to compile square.cpp bij choosing Build >> Choose current file.
    After that I try to compile main.cpp on the same way in Code::blocks.

    Roelof

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you're using C::B, you should make sure you have a project with both files in it. Then you compile the project, and not the files separately.
    Also make sure that the project is of a console type, not a Windows project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    That's did the trick.
    Still another problem.
    I have to use set(w) to make 2 collums.
    So I use this :
    Code:
    cout << left << setw(10) << teller << right << setw(10) << setprecision(3) << uitkomst
    			     << setprecision(prec) << endl ;
    But no 2 collums.

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  2. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  3. Calculating Square Root?
    By Neo1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 02:40 AM
  4. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM