Question

C Programming Exercise:

Write a simple program in C so that takes a four charachter string from a file and puts it into a two dimensional array. Make sure your program satisfies the following:

(i) uses a 2D Array(rows and columns)

(ii) get and put functions

(iii) use multiple functions/methods :

-In your Main function take input from file using the get functions.

In your Second function, call it print2DArray, which takes the string from the file in main method and passes it to this function so that you put the string into a 2D Array. Then print the array back.

(iiii) Make sure your program handles the End of File exception correctly.

See Image For Example:

Example: File1 contains the following string: TTTT commandLine: ./TwoDAray < file1 Here is ny table: Example2: File1 contains the following string: ABCD commandLine:$ ./TwoDArray < file1 Here is ny table: A B C D

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <stdio.h> // header file for i/p o/p functions
void print2DArray(char c[4]){ // function to store characters into 2 dimensional array
char array[2][2]; // 2D Array declaration
int k=0,i,j; // required variable declaration
for(i=0;i<2;i++) // copying characters
for(j=0;j<2;j++){
array[i][j]=c[k];
k++;
}
printf("\n\nHere is my table: \n\n");
for(i=0;i<2;i++){ // printing 2D Array
for(j=0;j<2;j++)
printf("%c ",array[i][j]);
printf("\n");
}
}
int main ( int argc, char *argv[] ){ // main function
char str[4]; // single dimensional array declaration
int i=0;
if (argc != 2) /* argc should be 2 for correct execution */  
printf( "usage: %s filename", argv[0] ); /* argv[0] is the program name, where if command line argument is not given than program name is printed*/
else {
FILE *f = fopen( argv[1], "r" ); // argv[1] is a filename in which we read
if (f==0) /* fopen returns 0, the NULL pointer, on failure */
printf( "Could not open file\n" );
else {
char x;
printf("%s contains the following string: ",argv[1]);
while ((x=fgetc(f))!=EOF){ // read one character at a time from file, stopping at EOF, which indicates the end of the file.  
printf( "%c", x ); // priting characters
str[i]=x; // assigning to characters into single dimensional array
i++;
}
fclose(f); // closing file
}
}
print2DArray(str); // calling function
}// end of main

OUTPUT:

file1.txt contains the following string: tttt Here is my table:

Add a comment
Know the answer?
Add Answer to:
C Programming Exercise: Write a simple program in C so that takes a four charachter string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Using C++ programming. Write a program that takes a string of input from the user and...

    Using C++ programming. Write a program that takes a string of input from the user and separates the string of words based on the premise that the string contains words whose first letter is uppercase. Then, display the phrase (or sentence) to a string in which the words are separated by spaces and only the first word of the phrase starts with an uppercase letter. For example, if the user enters "IAmTheTeacher", then the program would display: "I am the...

  • Question 3. Write a program that contains the following functions a) A function that takes in...

    Question 3. Write a program that contains the following functions a) A function that takes in as input the amount a person has to pay to buy a house, and the monthly payments made. Your function should display how much has been payed and how much is left to pay every month until the mortgage has been payed (Ignore interest and the like) b) A function that takes in as input the color of a car, and returns the number...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string...

    "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string functionality to count the number of vowels in this phrase. Q7. Write a function bool xor(bool A, bool B) which returns the Exclusive OR of boolean input variables A and B. Test your function by printing out the full truth table of the function. Q8. Write a method swap(char_all, int i, int i) which swaps element i and element j of a given character...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input....

    Exercise 3.1 (word count.py). Write a program that takes in a filename and string as input. Then print how many times that string appears inside the chosen file. If the file does not exist, continue asking for a filename until one is given that exists. Use your source code file as test input. Make sure to test files with that contain the same word multiple times. ? $ python3 word-count.py 2 Please enter a filename: wordcount.py 3 Please enter a...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT