1. Given the text file that is provided along with this quiz,(Question1.txt), answer the following questions to the best of your
ability. You may run the code if you wish to see it work.
a. Spend a few minutes looking through the code, then in a general
overview explain the purpose of the code in a few sentences.
b. Explain what the loop is doing and the purpose of the char c
line.
for (int i = 0; i < length; i++) {
char c = str[i];
c. There are 7 if statements in the user defined function, explain
the purpose of each one and how it achieves that.
2. Explain what this code does in terms of the array elements.
Provide a step-by-step explanation of how the code sorts the array in
ascending order using a bubble sort algorithm. Also, provide the sorted
array after the code snippet is executed.
int arr[] = { 5, 8, 2, 1, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
int* ptr = arr;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (*(ptr + j) > *(ptr + j + 1)) {
int temp = *(ptr + j);
*(ptr + j) = *(ptr + j + 1);
*(ptr + j + 1) = temp;
}
}
}
3. Given the following code snippet, answer the following.
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
char* ptr;
char string[] = “This week was a scorcher.”;
ptr = string;
ptr += 5;
printf(“%s”, ptr);
return 0;
}
a. What will be the output of this program? Explain why.
b. We are adding 5 in this example, what does that number signify in
relation to what will be printed?
c. What happens if your output the string (char string[])? Is it
modified in anyway?
4.
5.
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
bool isValidEmailAddress(char* str) {
int length = strlen(str);
bool hasAtSymbol = false;
bool hasDot = false;
int dotIndex = -1;
if (length == 0) {
return false;
}
for (int i = 0; i < length; i++) {
char c = str[i];
if (!(isalnum(c) || c == '@' || c == '.' || c == '_')) {
return false;
}
if (c == '@') {
if (hasAtSymbol) {
return false;
}
hasAtSymbol = true;
}
if (hasAtSymbol && c == '.') {
if (hasDot) {
return false;
}
hasDot = true;
dotIndex = i;
}
}
if (hasAtSymbol && hasDot && dotIndex > 0 && dotIndex < length - 1) {
return true;
}
return false;
}
int main() {
char email1[] = "example@example.com";
char email2[] = "invalid_email";
char email3[] = "abc@def";
char email4[] = "email@domain.";
printf("Email 1: %s\n", email1);
printf("Is Valid? %s\n", isValidEmailAddress(email1) ? "Yes" : "No");
printf("\n");
printf("Email 2: %s\n", email2);
printf("Is Valid? %s\n", isValidEmailAddress(email2) ? "Yes" : "No");
printf("\n");
printf("Email 3: %s\n", email3);
printf("Is Valid? %s\n", isValidEmailAddress(email3) ? "Yes" : "No");
printf("\n");
printf("Email 4: %s\n", email4);
printf("Is Valid? %s\n", isValidEmailAddress(email4) ? "Yes" : "No");
printf("\n");
return 0;
}