Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem #1:
- Create a program that takes a string as input when the program is started and prints
- the words of the sentence in reverse. You will need to use argc and argv in the main
- function. You should not need to declare your own array but use the argv array and
- a loop to present the input strings in the reverse order.
- Note: Recall that char* argv[] is an array of character pointers. So each input string
- occupies a row in the array.
- Problem #2:
- Compose a program that prompts the user to enter in 10 numbers and store them
- into an array of strings one at a time. The array should be a 2d array of characters
- that can hold 10 strings up to a length of 20 characters each. Handle the numerical
- input as strings and store them into the array (You don’t need to reference the 2nd
- dimension). Use a loop to print out the strings to verify you have your numbers
- stored as asci characters. Now loop through the array and use the atof() function to
- convert the string version of the numbers into a double type and add it to a sum
- variable. Print out the sum and end the program. Note: scanf() will read in strings
- from standard in until they encounter (a space, end of file, or newline/carriage
- return) gets and fgets will include those items, which makes scanf a better choice
- here
- SAMPLE OUTPUT:
- Enter in a number: 1
- Enter in a number: 2
- Enter in a number: 3
- Enter in a number: 4
- Enter in a number: 5
- Enter in a number: 6
- Enter in a number: 7
- Enter in a number: 8
- Enter in a number: 9
- Enter in a number: 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- Sum: 55.00
- /*** SKELETON CODE ***/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void)
- {
- char ary[10][20];
- double sum = 0.0;
- int i; //For the loops
- [YOUR CODE HERE]
- printf("Sum: %.2lf\n", sum);
- return 0;
- }//end main
- Problem #3:
- Create a program that prompts the user for the name of a text file (example:
- input.txt). Your program should verify if the file exists and report to the user if it does
- or if it does not.
- Problem #4:
- Analyze the following program. Figure what it is doing, and what each of its functions
- and if-else conditionals do. ONLY RUN THIS PROGRAM AFTER YOU HAVE FIGURED OUT ITS FUNCTION.
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- void FlushInput(void);
- bool FileExists(char*);
- bool CreateFile(char*, char*);
- int main(void){
- FILE* fIn = NULL;
- char textIn[300];
- char fileName[80];
- char saveFile = 'y';
- bool result = false;
- printf("Enter in some text to save:");
- if(fgets(textIn,300,stdin)){
- printf("Your entered in %d characters.\n", (int) strlen(textIn));
- printf("Enter in the name of a text file: ");
- fscanf(stdin,"%s",fileName);
- if(FileExists(fileName) == true){
- printf("The file exists, enter 'y' to overwrite:");
- FlushInput();
- scanf("%c", &saveFile);
- if(saveFile == 'y')
- result = CreateFile(textIn, fileName);
- else
- printf("You chose not to save the data.\n");
- }
- else{
- result = CreateFile(textIn, fileName);
- }//end if-else
- }
- else {
- printf("You did not enter in anything to save.\n");
- }
- if(result)
- printf("You successfully created a file named %s\n", fileName);
- else
- printf("You did not create a file named %s\n", fileName);
- return 0;
- }//end main
- bool FileExists(char* fileName){
- FILE* fIn = fopen(fileName,"r");
- if(fIn == NULL)
- return false;
- else{
- fclose(fIn);
- return true;
- }
- }//end function
- void FlushInput(void){
- while(getchar() != '\n');
- return;
- }//end function
- bool CreateFile(char* textIn, char* fName){
- FILE* fPtr = fopen(fName,"w");
- if(fPtr == NULL){
- printf("Error opening file.\n");
- return false;
- }
- else{
- fputs(textIn, fPtr);
- fclose(fPtr);
- return true;
- }//end if-else
- }//end function
Add Comment
Please, Sign In to add comment