Monday, May 23, 2011

5/23

This unit expanded on the various types of variables. Discuss the difference and usage of local variables, global variables, static variables and automatic variables. Post some snippets of code illustrating their use. 

Local variables : are used within a block such as 

int main()
{

int num = 20;
cout >> “please enter a number” >> endl;
cin << num << endl;
}

Or in a function (maybe)
Int number (int num)
{
Cin << num
}
This sets num to 20 for the main() function 
They cannot be used outside of their block they are initialized in.
We have used these in just about every program so far.
If we think of them as a tv show, they are characters that come in for one episode or the main theme of one episode.

Global variables: These are variables that are initialized before the main program body 
Example:

#include 
using namespace std;
int num = 20;
int main()
{
cout << num << endl;
This initializes num to 20 for the entire program.

To use the tv example, they are the main characters or main plot of the show
We have used them, not as much though

Static variables: These are almost the exact same thing as global variables, except you can declare them within a block by using:

Static datatype identifier
Or an example
Static int = 20;
Another thing to note is that as long as the program is “going”
A good example is X on page 392
Void test()
Static int x = 0;
X = X + 2;
Cout << “inside test x = “ << x << endl;
Inside test x = 2
Inside test x = 4
Inside test x = 6
Ecrt..
What this does is increase x by 2 each time it executes around .
We have not used these that I am aware of.


Automatic variables: The book describes these as pretty much the same as local variables. The only addition from local variables that is mentioned is that they are initialized to 0.
From other wibsites that I found they said that they are not normally used by “human” programmers but program generators

Wednesday, May 18, 2011

I think its 5/18...

Choose one of the many topics that we have discussed so far in this course. Selection structures, repetition structures, functions, I/O etc. What have you learned about these? Have there been any surprises?


Functions in C++ are a bit different than what I expected them to be. I assumed that they were saved in a different area and when you call them you have to add the path into that call.
The prototype deals with the situation quite nicely though. I understand many people’s concerns and confusion about having to declare the variables for that function so late in the program, or that it is considered good practice to put the functions after the program. I rather like it though, for myself it seems cluttered to have something opposed to the main function coming before it.

I feel I can do better on repetition structures, I never was good at them, have a hard time deciding what loop to use and how get the loop to do what I want it to do and how many times.




My midterm discussion board for programming 1

I've decided to post some things for my business classes as well, but only major project.


For business communications we are to do a 15-20 min presentation on a topic regarding communications, this is a 2 person assignment.


We have chosen social media as our topic, I dont have much more, we just got it 2 days ago.


There is not much for intro to business, unless I can find out how to upload to a pdf doc(our final project is a paper on a any topic(mine = what defines a good leader) that has to be 5-10 pages) I'll look into it. 




For my programming 1 class our project of the week is to create a program to count the number of vowels in a sentence using void function. I think I'm almost done with it but have 1 issue with the counting loop. 

Monday, May 9, 2011

2 POSTS TODAY!!!!

So I have midterms this week, which means there will not be much to post. However we are to use the discussion board to write the following(I will be uploading my response):
Choose one of the many topics that we have discussed so far in this course.  Selection structures, repetition structures, functions, I/O etc.  What have you learned about these?  Have there been any surprises?



TODAY!!!.. IS ^THAT^ DATE!!!!

I finished my programs about 2 days ago...

first one, we were supposed to a add on a few things to an example in the book. The example was to find a a palindrome(dont know if that is spelled right). One addition that my instructor put on us is to not accept user input. soooooo...here is the code....
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

bool isNumPalindrome(int num);

int main(int argc, char *argv[])
{
 cout << "answers in binary" << endl;
 cout << "the answer is: " << isNumPalindrome(646) << endl;
          
          
    system("PAUSE");
    return 0;
    
    
    
}




bool isNumPalindrome(int num)
{
     int pwr = 0;
     
     if (num < 10)
        return true;
     else
     {
         while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
         pwr++;
         while (num >= 10)
         {
               int tenTopwr = static_cast<int>(pow(10.0, pwr));
               
               if ((num / tenTopwr)  != (num % 10))
                  return false;
               else
               {
                         num = num % tenTopwr;
                         num = num / 10;
                         pwr = pwr - 2;
                         }
                         }
                         return true;
                         }
                         }

some things I feel I could have dont is put in a switch system so that it output "the number is not a palindronme" or "it is a palindrom" instead of binary
something like

      switch (something here)
    case 0:
           cout << "num is not a palin" << endl;
    case 1: 
           cout << "num is a palin" << endl;

the above isnt perfect; just psudocode.

we were to make two other functions, one that output a double when it was passed an int variable

and the other was to simply install a prototype

first


#include <cstdlib>
#include <iostream>

using namespace std;

double check (int a, double b, double c);
int main(int argc, char *argv[])
{
 
    check(1, 2.2, 2.3);
    system("PAUSE");
    return EXIT_SUCCESS;
}

double check(int a, double b, double c)
{
          cout << "please enter a number" << endl;
          cin >> a;
          cout << "please enter a decimal number" << endl;
          cin >> b;
          cout << "please enter another decimal number" << endl;
          cin >> c;
         
}

quick note, the above didnt have to output anything


second 
double findabd ( int a, double b)
{

return (double)
}
not much to say on that one

Thursday, May 5, 2011

5/5

So I've been hit with a recent wave of the "holy crap I have a lot of homework"s
But this week in programming we're going through functions, the first part is value returning functions(next week is void functions).
We have to write one program and 2 individual functions, I'll get those up when I get the last one finished, one is complete, one is a bout 75% done and I'm wondering why the compiler says things are undeclared in my last one.

Sunday, May 1, 2011

5/1

so my parents thought it would be cool to go on vacation during the school week -_-

anyway ive been working on 2 different programs in that time


#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
int num;
int x;
int root;
bool prime;

cout << "Please enter a number: ";
cin  >>  num;
cout  <<  endl;

root  =  pow(num, 0.5);

for  (x=2;  x <=  root;  x++)
{
if (num % x == 0)
{
cout << "number is not prime" << endl;
prime = false;
}
else
{
cout << "number is prime" << endl;
prime = true;
}
}


   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


for this one you are input a positive number and the output is supposed to tell you if the number you input is a prime number(if you cant tell this week is loops)


#include <cstdlib>
#include <iostream>
#include <iomanip>


using namespace std;



int main(int argc, char *argv[])
{
    int counter;
    int num;
    int zero = 0;
    int even = 0;
    int odd = 0;
    int N = 10;
    int sum = 0;
   
    cout << "please enter " << N << " integers, "
         << "posative, negative, or zeros."
         << endl;
       
    cout << "the numbers you entered are:" << endl;
   
    for (counter = 1; counter <=N; counter++)
    {
                 cin >> num;
                 cout << num << " ";
               
               
                 switch (num % 2)
    {
    case 0:
         even++;
         if (num == 0)
            zero++;
           while (num == even)
           {
                 sum = sum + num;
                 cin >> num;
                 }
         
         break;
         case 1:
         case -1:
              odd++;
         }
              }
       
         cout << endl;
         
         cout << "there are " << even << " evens, "
              << "which includes " << zero << " zeros. "
              << endl;
         cout << "the number of odd numbers is: " << odd
              << endl;
       

   

   

   
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

this one was tough, you have to enter a set of numbers and then have it output the sum of the even numbers and odd numbers and how many zeros there are