Tag Archives: array

C++ || Struct – Add One Day To Today’s Date Using A Struct

This program displays more practice using the structure data type, and is very similar to another program which was previously discussed here.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Functions
Passing a Value By Reference
Integer Arrays
Structures
Constant Variables
Boolean Expressions

This program utilizes a struct, which is very similar to the class concept. This program first prompts the user to enter the current date in mm/dd/yyyy format. Upon obtaining the date from the user, the program then uses a struct implementation to simply add one day to the date which was entered by the user. If the day that was entered into the program by the user falls on the end of the month, the program will”roll over” the incremented date into the next month. If the user enters 12/31/2012, the program will “roll over” the incremented date into the next calendar year.

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:
Note: The code was compiled 6 separate times to display the different outputs its able to produce

Please enter today's date in mm/dd/yyyy format: 1/18/2012
The next day is 1/19/2012
-------------------------------------------------------------------

Please enter today's date in mm/dd/yyyy format: 7/31/2012
The next day is 8/1/2012
-------------------------------------------------------------------

Please enter today's date in mm/dd/yyyy format: 2/28/2012
The next day is 3/1/2012
-------------------------------------------------------------------

Please enter today's date in mm/dd/yyyy format: 13/5/2012
Invalid input...
Program exiting....
-------------------------------------------------------------------

Please enter today's date in mm/dd/yyyy format: 2/31/2012
Invalid day input - There is no such date for the selected month.
Program exiting....
-------------------------------------------------------------------

Please enter today's date in mm/dd/yyyy format: 12/31/2012
The next day is 1/1/2013

C++ || Searching An Integer Array For A Target Value

Here is another actual homework assignment which was presented in an intro to programming class.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
For Loops
Functions
If/Else Statements

This is a small and simple program which demonstrates how to search for a target value that is stored in an integer array. This program prompts the user to enter five values into an int array. After the user has entered all the values, it displays a prompt asking the user for a search value. Once it has the search value, the program will search through the array looking for the target; wherever the value is found, it will display the index location. After it displays all the locations where the value is found, it will display the total number of occurrences the search value was found within the array.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:
Note: The code was compiled 3 separate times to display the different outputs its able to produce

Please enter 5 integer values:
#1: 12
#2: 12
#3: 12
#4: 12
#5: 12
Please enter a search value: 12

12 was found at array index #0
12 was found at array index #1
12 was found at array index #2
12 was found at array index #3
12 was found at array index #4

The total occurrences of value 12 within the array is: 5
-------------------------------------------------------------------------

Please enter 5 integer values:
#1: 12
#2: 87
#3: 45
#4: 87
#5: 33
Please enter a search value: 87

87 was found at array index #1
87 was found at array index #3

The total occurrences of value 87 within the array is: 2
-------------------------------------------------------------------------

Please enter 5 interger values:
#1: 54
#2: 67
#3: 98
#4: 45
#5: 98
Please enter a search value: 123

The total occurrences of value 123 within the array is: 0

C++ || Find The Prime, Perfect & All Known Divisors Of A Number Using A For, While & Do/While Loop

This program was designed to better understand how to use different loops which are available in C++.

This program first asks the user to enter a non negative number. After it obtains a non negative integer from the user, the program will determine if the inputted number is a prime number or not, aswell as determine if the user inputted number is a perfect number or not. After it obtains its results, the program will display to the screen if the user inputted number is prime/perfect number or not. The program will also display a list of all the possible divisors of the user inputted number via cout.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Do/While Loop
While Loop
For Loop
Modulus
Basic Math - Prime Numbers
Basic Math - Perfect Numbers
Basic Math - Divisors


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

Enter a number: 8128

Input number: 8128
8128 is not a prime number.
8128 is a perfect number.
Divisors of 8128 are: 1, 2, 4, 8, 16, 32, 64, 127, 254, 508, 1016, 2032, 4064, and 8128

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 6

Input number: 6
6 is not a prime number.
6 is a perfect number.
Divisors of 6 are: 1, 2, 3, and 6

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 241

Input number: 241
241 is a prime number.
241 is not a perfect number.
Divisors of 241 are: 1, and 241

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 2012

Input number: 2012
2012 is not a prime number.
2012 is not a perfect number.
Divisors of 2012 are: 1, 2, 4, 503, 1006, and 2012

Do you want to input another number?(Y/N): n
------------------------------------------------------------
Press any key to continue . . .

C++ || Whats My Name? – Using a Char Array, Strcpy, Strcat, Strcmp, & Strlen

Here is another actual homework assignment which was presented in an intro to programming class.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Functions
Strcpy
Strcat
Strcmp
Strlen

This program first prompts the user to enter their name. Upon receiving that information, the program saves input into a character array of size 15. The program then asks if the user has a middle name. If they do, the user will enter a middle name. If they dont, the program proceeds to ask for a last name. Upon receiving the first, [middle], and last names, the program will use strcpy and strcat to copy/append the first, [middle], and last names into a completely new char array titled “fullname.” Lastly, if the users’ first, [middle], or last names are the same, the program will display that data to the screen via cout. The program will also display to the user the number of characters their full name contains using strlen.

NOTE: On some compilers, you may have to add #include < cstring> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

CHAR ARRAY
Unlike integer arrays, a loop is not necessary in order to input data into a char array. This is highlighted on lines 32, 76, 79, and 125.

FUNCTIONS
Notice lines 18, 19, 69 and 119. When dealing with arrays, in order to pass variables to different functions, you need to include brackets[] letting the compiler know that you are passing an array variable. If you do not declare the functions as so, you will get a compiler error.

STRCPY/STRCAT
These functions are highlighted on lines 81-85, and 127-129. Read the comments within the code on those selected lines, they are helpful.

STRCMP
This compares two strings together to determine if they are the same. This is displayed on lines 92, 96, 100, and 136 when comparing the first, [middle], and last names together for sameness.

STRLEN
This finds the length of the current string, as noted on lines 89 and 133.

Once compiled, you should get this as your output:
Note: The code was compiled five separate times to display the different outputs its able to produce


Please enter your first name: My
Do you have a middle name?(Y/N): y
Please enter your middle name: Programming
Please enter your last name: Notes

Your full name is My Programming Notes
The total number of characters in your name is: 18
-----------------------------------------------------------------------

Please enter your first name: Programming
Do you have a middle name?(Y/N): N
Please enter your last name: Notes

Your full name is Programming Notes
The total number of characters in your name is: 16
-----------------------------------------------------------------------

Please enter your first name: Notes
Do you have a middle name?(Y/N): y
Please enter your middle name: Notes
Please enter your last name: Notes

Your full name is Notes Notes Notes
The total number of characters in your name is: 15
Your first and middle name are the same
Your middle and last name are the same
Your first and last name are the same
-----------------------------------------------------------------------

Please enter your first name: My
Do you have a middle name?(Y/N): N
Please enter your last name: My

Your full name is My My
The total number of characters in your name is: 4
Your first and last name are the same
-----------------------------------------------------------------------

Please enter your first name: My
Do you have a middle name?(Y/N): z

Please press either 'Y' or 'N'
Program exiting...