CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2016
    Posts
    2

    squared numbers is in range

    Hey,
    Won't lie this is a teachers assigment but i became kinda sad, cause i have no idea how to do last part of it.
    I'm supposed to find all pairs of positive integers whose squares add up to a number in the given range.

    Well im stuck with that...
    I need to sum all the number squares of for example:
    1 + 1 = 2
    1 + 2 = 5
    1 + 3 = 10
    1 + 4 =17
    2 + 2 = 8
    and so on...
    And when user enter a range of 1-10 for example...
    I need to print out the numbers that makes sum that is in the users range if that makes sence.

    range 1-10
    1 + 1 = 2
    2+2 =8
    1 + 3 = 10
    1+2 = 5

    range 5-15
    1+9 = 10
    1+2 = 5
    2+3 = 13

    I try do this with while loop, had 0 success. I did this on paper but i have no idea how to start to write it.

    Code:
    12 + 12 = 2     print to screen and increment right square
    12 + 22 = 5     print to screen and increment right square
    12 + 32 = 10    print to screen and increment right square
    12 + 42 = 17    17 is > 10, so increment left square and right square = 1
    22 + 12 = 5     print to screen and increment right square
    22 + 22 = 8     print to screen and increment right square
    22 + 32 = 13    13 is > 10, so increment left square and right square = 1
    32 + 12 = 10    print to screen and increment right square
    32 + 22 = 13    13 is > 10, so increment left square and right square = 1
    42 + 12 = 17    The program would stop here



    Code:
      int main(){
         	int * intervals, * kvadrats;
    		int maslielums,m,n,i = 0;
    		int mazakais = 0;
         	bool parbaude1;
         	char at = 'j';
        
         	while(at == 'j'){
        //checks if m is entered correct
          do {
               parbaude1 = false;
               cout<<"Intervala m veribu: ";
               cin >> m;
               if (!cin.good()) {
               cout << "Ievadiet tikai naturalu skaitli!" << endl;
               cin.clear();
               cin.ignore(256,'\n');
               parbaude1 = true;
             }//cannot be negative
                 if(m<0) {
                 cout << "Skaitlis nedrikst but negativs: " << endl;
                 parbaude1 = true;
               }
        } while(parbaude1 == true);
        
        // checks if n is entered correctly
        do {
               parbaude1 = false;
               cout<<"Intervala n veribu: ";
               cin >> n;
               if (!cin.good()) {
               cout << "Ievadiet tikai naturalu skaitli!" << endl;
               cin.clear();
               cin.ignore(256,'\n');
               parbaude1 = true;
             }// nedrikst but negativs!
                 if(n<0) {
                 cout << "Skaitlis nedrikst but negativs: " << endl;
                 parbaude1 = true;
               }
        } while(parbaude1 == true);
         
         // Check which of the number n or m is smaller.
    	  if (m>n){
          intervals = new int[m-n+1];
          maslielums = m-n; // gives out users range
          mazakais = n; // smallest number that user entered
          }
              else{ 
          intervals = new int[n-m+1];
          maslielums = n-m; // gives out users range
          mazakais = m; // smallest number that user entered
            }
        //fils out range with numbers  
        for (int i=0; i<=maslielums; i++){
        intervals[i] = mazakais;
        mazakais++; 
    }
        
        
    		
       cout << endl << "Ja velies atkartot programmu ievadi 'j',bet ja beigt tad ievadiet jebko citu!"<< endl; 
       cin >> at;
       delete[] intervals;
    }
    return 0;
    }

  2. #2
    Join Date
    Oct 2016
    Posts
    2

    Re: squared numbers is in range

    12 + 12 = 2 print to screen and increment right square
    12 + 22 = 5 print to screen and increment right square
    12 + 32 = 10 print to screen and increment right square
    12 + 42 = 17 17 is > 10, so increment left square and right square = 1
    22 + 12= 5 print to screen and increment right square
    22 + 22 = 8 print to screen and increment right square
    22 + 32 = 13 13 is > 10, so increment left square and right square = 1
    32 + 12 = 10 print to screen and increment right square
    32+ 22 = 13 13 is > 10, so increment left square and right square = 1
    42+ 12= 17 The program would stop here
    If the user enters 1-15 for example.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: squared numbers is in range

    i have no idea how to start to write it.
    The basic algorithm is very simple. Consider

    Code:
    	for (int x = start; x <= fin; ++x)
    		for (int y = start; y <= fin; ++y) {
    			int z = x * x + y * y;
    			if (z >= n && z <= m)
    				cout << x << " " << y << " " << z << endl;
    		}
    where n is the lower range limit and m is the upper range limit. start and fin are the start and finish numbers for the loops - which need to be determined. Also this algorithm can be improved for efficiency once z is greater than m. But I'll leave this an an exercise!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured