Author Archives: carlos green

mastery 24

++  

Arrays are a good way to store a determined amount of numbers given by the user. You can either enter the size of the array, and that will be the maximum f numbers that will fit in. Just so, you can create a variable in order to ask the user for the size of the array and that way you can ask as many numbers as the user wants to provide. When using arrays in functions, you need to replace your variables in the main() code, so you can ask the user for the size of the array and then ask for each number using a “for” loop or a “do while” loop.

here is an example:

<iostream>

<cstdlib>

 using namespace std;

 

 int average(int x[], int size){

   int sum=0, average;

 

   for(int i=0; i<size; i++){

     sum=sum+x[i];

   }

   average=sum/size;

   return average;

 

 }

 int main(){

   int size;

   int a[size];

   cout<<“give me the amount of numbers “<<endl;

   cin>>size;

   for(int i=0; i<size; i++){

     cout <<“give me a number “;

     cin>> a[i];

 }

 cout<<average(a,size);

 }

WSQ13

 

Here is my code for wsq13:

The babylonian method to get the square root of a number was first created by the babiloninas. The method works by creating to integer values that can be an overestimate of the sqrt of a number, therefore, the average of those two overestime values will result in the sqrt of the number. 

The program creates the overestimate number by dividing the number given by the user and then creating a  new variable to store the result of the division in order to use it inside the loop. 

WSQ12

 

Here is my code for wsq12. This program asks the user for two integer numbers and then prints the great common factor from those numbers. The program uses one for loop which creates a counter that works while being minor equal to both integer numbers. After that, it uses an “if” conditional to asign z to gcf which is a variable created to calculate the great common factor. “Z” increments one by one until it gets to a number that is capable of being divided by the two numbers given by the user, and that is why the “if” conditional asigns gcf = “z” when dividing z%first and second, and getting 0 as an answer. 

WSQ10

This program is created using arrays in order to store a maximum of numbers entered by the user.

After that, the stored numbers can be used to create a sum of them and the average of them as well. This works because we created a function that stored 5 numbers (array [5]) in order to be able to ask the user for 5 numbers to be stored.

here is the code with notes to be better understanded:

<iostream>

using namespace std;

 

int main(){

int array [5]; //declaring the array with a maximum of 5 values

 

for (int i=0; i<5; i++){ //the first loop will sotre the values from 0 to 4 (including 0)

  cout << “please give us a number: “;

  cin >> array [i]; // the program will read the values that the user enters until value # 4

 

}

for (int i=0; i<5; i++){ // the program will show the values that the user entered

  cout << array [ i ]<<endl;

}

int sum = 0;//fisrt variable to sum the values from the array

int average,sum1;

for (int i = 0; i < 5; ++i)// this loop will give the program all the values from the array in order to sum them

  sum+=array[i];// sums the values given by the array

average = sum/5; //gets average

cout<<” Average:”<<average;

 

 

return 0;

}

 

WSQ11

#<—-notpalindrome

This program was created for the user to give a value and see if it is a palindrome or not.

the program first asks for a number entered by the user, after this, we called a function that converts the integer to a string value. After that, we invert the string so the number looks backwards and we convert it to “int” again. After this new int is created, we compare the original one with the new one (which is backwards) and see if they are the same. If they are indeed de same, then we tell the program to print a message that sais “the number is a palindrome”. To make it look even better, we asked the user for two numbers and then we printed the ones that were palindromes using the first steps, if it wasn’t a palindrome, then we asked the user if he wanted to see if it could be transformed to palindrome. To do this, we created another function that inverts the integer and adds it to its original, (in order to create the new palindrome) and we printed it to the user. If not even like that was the number able to transform into a palindrome, then  we printed a message that said it couldn’t be transformed.

here is the code with notes to be understanded:

<iostream>

<sstream>

<stdio.h>

“string.h”

<algorithm>

using namespace std;

 

int main (){

  int a, b, range, pal;

  string answer;

  int counter = 0;

  cout <<“please enter a value “;

  cin >> (a);

  cout <<“please enter the upper value”<<endl;

  cin >> (b);

 

  range = (b-a);//range of numbers between the ones given by the user

 

  cout <<“the range of the numbers given is  “<< range <<”  n”;

 

 

   for (int i=a; i<=b; i++) {

     ostringstream ostr; //output string stream

     ostr << i; //use the string stream just like cout,

     //except the stream prints not to stdout but to a string.

     std::string textnumber = ostr.str();

     string textnumber2 = textnumber;

     reverse(textnumber.begin(),textnumber.end());

      if (textnumber == textnumber2){

        cout <<” this number is a palindrome!n”<<textnumber<<endl;

      }else {

        cout <<“this number isnt a natural palindrome! n”<<textnumber2<<endl;

 

              //here is where the rest of the code needs to go

                   string str = textnumber;

                   std::stringstream s_str( str );//converts string to int value

                   int r;//new int value asigned for str

                   s_str >> r;//gives r the value of str in int

 

                   int newpalindrome =(r + i);// sums the original digit with its inverse

                   cout <<” “<<newpalindrome << ” “;

                   for (int i=a; i<=b; i++) {

                     ostringstream ostr; //output string stream

                     ostr << newpalindrome; //use the string stream just like cout,

                     //except the stream prints not to stdout but to a string.

                     std::string newpalindrome1 = ostr.str();//creates the string variable that will include the value

                     //                                        of “newpalindrome”

                     string newpalindrome2 = newpalindrome1;// saves the original text to be able to compare both

                     reverse(newpalindrome1.begin(),newpalindrome1.end());

                      if (newpalindrome1 == newpalindrome2){

                        cout <<” this number became a palindrome due to the sum of the original nonpalindrome with its inverse!n”<<newpalindrome1<<endl;

 

 

                    }

                    else {

                      cout <<“sorry, this number is a natural lycherel”<<newpalindrome1 <<endl;

                    }

                    counter = counter + 1;

                  }while (counter < 1);

                  }

 

 

 

             }

 

 

 

 

             return 0;

        }

WSQ09

 

In this code, I created two variables called n and i. Then, i asked the user to enter an integer value for n, and asigned that value for “i”. After that, i created another variable called “fact” which then would be multiplied by “i” in relation to the “for loop” =((i=n ; i>=1 ; i–), in order to create the factorial. 

# include <iostream>

using namespace std;

 

int main () {

 

int i, n;

int fact=1;

cout<<“Enter the Value of n “<<endl;

cin>>n;

for(i=n ; i>=1 ; i–)

fact = fact*i;

cout<<“Factorial = “<<fact;

 

return 0;

}

WSQ08

.0 

here is my code for wsq08 

<iostream>

using namespace std;

 

int sum(int a, int b) {

return a+b;

}

int diff(int a, int b) {

return a-b;

}

int product(int a, int b) {

return a*b;

}

int div(int a, int b) {

return a/b;

}

int remainder(int a, int b) {

return a%b;

}

int main(){

  int a, b;

  cout <<“enter the first value”<<endl;

  cin>> (a);

  cout <<“enter the second value”<<endl;

  cin>> (b);

 

  cout <<“the sum of the numbers is “<<sum (a,b)<< endl;

  cout <<“the difference of the numbers is “<<diff (a, b)<< endl;

  cout <<“the product of the numbers is “<<product (a, b)<< endl;

  cout <<“the integer divition of the numbers is “<<div (a, b)<< endl;

  cout <<“the remainder of the numbers is “<<remainder (a, b)<< endl;

 

  return 0;

}

mastery 16

 

here is my code for mastery 16 showing the use of “else” as a conditional.

here is my code for masteries 15 and 16

<iostream>

using namespace std;

 

int main(){

  int a, c, d;

  char b;

 

  cout <<“does it move? 1) yes 2) no”;

  cin >> (a);

  if (a == 1) {

    cout << “should it move? 1)yes 2)no”;

    cin>>(c);

    if (c == 1) {

      cout << “then no problem 8)”;

 

    }

    else {

      cout << “then use duct tape”;

     }

 

 

 

    }

    else {

      cout <<“Should it move? 1)yes 2)no”;

      cin >>(d);

      if (d==1) {

        cout << “then use DW40”;

    }

    else {

      cout <<“then no problem 8)”;

    }

  }

  return 0;

}

Mastery13


Mastery13

Mastery13


when coding in c++ it’s important to use libraries so the program knows what to do…
An example of a library could be or
here is an example:

Mastery 10


Mastery 10

Mastery 10

&output