These are the final programs and I was wondering if I could get some help on me final review before submission. Can somebody please tell me what glaring omissions and errors, if any, are present in this code? Thanks again
1) Create a 2-by-3 two-dimensional array of integers and fill it with data. Loop through the array and locate the smallest value stored. Print out the smallest value as well as its row and column position in the array.
Code:
#include <stdio.h>
main()
{
int min, minrow, mincol, size, b, c;
int array[2][3] = {{10, 12, 33},{11, 28, 32},};
for ( c = 0 ; c < size ; c++ )
{
if ( array[b][c] < min )
{
min = array[b][c];
minrow = b;
mincol = c;
}
}
printf("Smallest Value: %d.\n", min);
printf("Value located in Row: %d Column: %d.\n", minrow, mincol);
return 0;
}
2) Prompt the user for 3 sentences of text. Pass these pieces of text into a function connect() which will connect all three sentences into one long sentence. Pass the combination sentence back to the main program, where it is printed.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *combineString(char *string1, char *string2, char *string3)
{
char *CombinedString;
CombinedString = malloc(200);
CombinedString[0] = 0;
strcat(CombinedString, string1);
strcat(CombinedString, " ");
strcat(CombinedString, string2);
strcat(CombinedString, " ");
strcat(CombinedString, string3);
return (CombinedString);
}
int connect ()
{
char string1[50];
char string2[50];
char string3[50];
printf("Please enter first sentence: ");
scanf(" %19[^\n]", string1);
printf("Please enter second sentence: ");
scanf(" %19[^\n]", string2);
printf("Please enter third sentence: ");
scanf(" %19[^\n]", string3);
char *combined = combineString(string1, string2, string3);
printf("Combined sentence is: %s\n\n", combined);
free(combined);
getchar();
getchar();
return(0);
}
3) Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.
Code:
#include <stdio.h>
int main ( void )
{
char filename[256];
printf("Filename: ");
scanf("%s", &filename);
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char line [ 999 ];
while ( fgets ( line, sizeof line, file ) != NULL )
{
printf(__LINE__, fputs (line, stdout ));
}
fclose ( file );
}
else
{
perror ( filename );
}
return 0;
}