Write a C program using (1) arrays and then (2) pointers with malloc() and free(
ID: 3873506 • Letter: W
Question
Write a C program using (1) arrays and then (2) pointers with malloc() and free() functions to
(A1) Open a file, read and print line by line.
(A2) Open a file, read only integers (max 300 numbers) from that file into an array, and print out the array in the reverse order.
(A3) Open a file, read all numbers (floating point is fine) into an array and return those that are strictly less than their two neighbors.
(A4). Get two integers n and m from the user, generate two n-by-m matrices and add/subtract one from another. Print out both matrices and the resulting one.
Explanation / Answer
Hi,
There are 4 different program questions in this, as per chegg policy, we are supposed to answer only one per answer, please post others as a separate question, we will be happy to assist you :)
answering the 1st part here, below is the code with comments to help you understand.
#include <stdio.h>
int main ()
{
char filename[] = "file.txt";//your file name here
FILE *file = fopen ( filename, "r" );//open for reading
if ( file != NULL )
{
char line [128]; // max size of line to be given here
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line*/
{
fputs ( line, stdout ); // printing the line
}
fclose ( file );//closing the file pointer
}
else
{
printf("error while opening file");
}
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.