Function that takes file pointer and 2D array - c

I want to write a simple program that creates a 2D array and then prints it out.
I want to write a function that reads from an input file and creates the array.
Then, in the main function, I want to call the function to make the array and then print it out.
I know this is basic stuff but I can't find this answer anywhere in my textbook or online.
The input file is just a text file written exactly like the array' 12 row with 8 random numbers in each row.
#include <stdio.h>
void makeArray(FILE*ptr, int array[12][8]){
int i,j;
ptr = fopen("scores.txt", "r");
for (i=0; i<12; i++){
for (j=0; j<8; j++){
fscanf(ptr, "%d", array[i][j]);
}
}
}
int main(){
int i, j;
int scores[12][8];
FILE*input;
void makeArray(input, scores);
for (i=0; i<12; i++){
for (j=0; j<8; j++){
printf("%d\t", scores[i][j]);
}
printf("\n");
}
return 0;
}

Related

Why do I get segmentation error when the number of rows/columns exceed 5?

I am trying to solve the following problem -
Given a matrix of size M * N. Find the transpose of the matrix.
I intend to store the M*N matrix in an array of pointers and then use a function to return another array of pointers that contains the transpose of the original matrix. Here is the code I used (the code is in C language) -
#include<stdio.h>
#include<stdlib.h>
int **transpose(int **a, int m, int n);
int main()
{
int **array, m, n;
scanf("%d%d", &m, &n);
array=(int **)malloc(m*sizeof(int)); //allocating rows
for(int i=0; i<m; i++){
*(array+i)=(int *)malloc(n*sizeof(int)); //allocating columns
}
int i, j;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", *(array+i)+j); //reading input
}
}
int **tranp=transpose(array, m, n);
for(i=0; i<n; i++){
for(j=0; j<m; j++){
printf("%d ", *(*(tranp+i)+j)); //printing output
}
printf("\n");
}
return 0;
}
int **transpose(int **a, int m, int n)
{
int **b;
b=(int **)malloc(n*sizeof(int)); //allocating n rows for transpose
int i, j;
for(i=0; i<n; i++){
*(b+i)=(int *)malloc(m*sizeof(int)); //m columns for transpose
}
for(i=0; i<n; i++){
for(j=0; j<m; j++){
*(*(b+i)+j)=*(*(a+j)+i); //b[i][j] is assigned a[j][i]
}
}
return b; //the pointer to b[0][0] is returned
}
I first declared an array of pointers int **array and used the standard library malloc function to dynamically allocate M rows and N columns. Then I read the input and used the function transpose that has a return type int ** to generate the transpose of the array and assign it to the pointer tranp and print the transposed array.
However, this code works perfectly fine and prints the transposed array for M<5 and N<5. Whenever M>=5 or N>=5, I get the following message -
Process returned -1073741819 (0xC0000005)
I looked up about it and apparently (0xC0000005) is the error code returned because I am illegally accessing memory locations. I do not understand where have I gone wrong here. Why is it that this code works for all M and N less than 5 and fails otherwise?

I need to create a number pattern program in C for my school assignment. Pattern- 1 141 14941

The pattern is
___1
__141
_14941
I have tried to some extent but looks like i have created a odd number pattern program
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=1; i<=N; i++)
{
// Prints the first part of pattern
for(j=1; j<=(i*2)-1; j+=2)
{
printf("%d", j);
}
// Prints the second part of pattern
for(j=(i-1)*2-1; j>=1; j-=2)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
I can see that the pattern has numbers that are off by odd numbers such as 3 and 5 respectively.
But i cant seem to grasp exactly how to do it.
Any help is much appreciated.
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=0; i<N; i++)
{
//For printing Spaces
for(int sp=0;sp<N-i;sp++){
printf(" ");
}
/*For printing the numbers
from left till the middle column*/
for(int j=1;j<=i+1;j++){
printf("%d",j*j);
}
/*For printing the numbers
from the strict right to the middle column
and till the end*/
for(int j=1;j<i+1;j++){
printf("%d",j*j);
}
printf("\n");
}
return 0;
}

Dynamic Memory Allocation Of an Array in C [duplicate]

This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 1 year ago.
I have been trying to write a simple code wherein the array is allocated dynamically. Every time I specify the side of the array as n(suppose 4) and proceed to type the given input, it takes exactly n+1(5 in this case) inputs from me but as the output, it prints n(4) elements.
Here's the main function I wrote:
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++) {
scanf("%d ", &arr[i]);
}
for(int i=0 ; i<n; i++) {
printf("%d ", arr[i]);
}
}
I've also tried doing the code by initializing i in the first loop as 1, and in that way it takes exactly n inputs but it gives a weird output, something like this:
7953616 1 2 3
in those two lines
scanf("%d ", &arr[i]);
printf("%d ", arr[i]);
you have %d instead of %d
also you need to use free() from stdlib library as after you finish using the pointer you need to free the memory in order to reuse it again otherwise this will happen
final code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++)
scanf("%d", &arr[i]);
for(int i=0 ; i<n; i++)
printf("%d", arr[i]);
free(arr);
}

problem with 2d array mallocation (segmentation fault)

I tried to malloc a 2d array, but it gdb gives me Segmentation fault in line 8. It seems like something is wrong with if (A[i+x][j+y]!=A[i][j].
By the way: This program should print out the dimensions of the maximum square-sized sub-matrix of an input matrix, such that all its digits are equal matrix, such that all its digits are equal.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int same_surrounding(int **A, int i, int j, int size){
for (int x=0; x<size; x++){
for (int y=0; y<size; y++){
if (A[i+x][j+y]!=A[i][j])
return 0;
}
}
return 1;
}
int main(){
int n, i, j, sub, max_sub;
int **A;
scanf("%d", &n);
A=malloc(sizeof*A*n);
for (i=0; i<n; i++){
A[i]=malloc(sizeof**A*n);
}
for (i=0; i<n; i++){
for (j=0; j<n; j++){
scanf("%d", &A[i][j]);
}
}
for (i=0; i<n; i++){
for (j=0; j<n; j++){
sub=1;
while (sub<n&&same_surrounding(A, i, j, sub)){
if (sub>max_sub)
max_sub=sub;
sub++;
}
}
}
printf("%d", max_sub);
for (i=0; i<n; i++){
free(A[i]);
}
free(A);
return 0;
}
There are mainly 2 things wrong with this code:
Your program is checking for submatrices beyond the dimension of input matrix n. for example, if n=5,i=3,j=0 and sub=3 you can easily see that your code is checking for numbers in A[i+x][j+y] i.e. upto A[5][2] which is out of bound of A. You need to check this before sending it to same_surrounding(). For that all you need to do is slightlt change the while loop:
while (sub<=n && i+sub<n && j+sub<n && same_surrounding(A, i, j, sub)){...}
Always initialize your variables. Here you didn't initialize max_sub variable. When you don't initialize your variables, it contains random garbage value. For example if the garbage value is 478231 and you are looking for largest square matrix of 5x5 matrix, you'll never reach the condition if (sub>max_sub) and as a result max_sub will never update. That's why always initialize your variable with a safe value, for example, max_sub=0.
Also, as a warning, you should use sizeof() instead of explicitly writing the number of bytes necessary for memory allocation. Because they might change depending on computer architecture.
A=(int**) malloc(sizeof(int*)*n);
for (i=0; i<n; i++){
A[i]=(int*)malloc(sizeof(int)*n);
}
Here's the working version of your code. All I did was some { } cleanup. If you have just one statement under for, while, if etc., you don't really need the braces.

Why does scanf function asks for an extra input?

**#include <stdio.h>
#define SIZE 3
void scanA(int array[SIZE][SIZE]); // prototyping
int main()
{
int myarray[SIZE][SIZE];
int i,j;
printf("Please enter the array: \n");
scanArray(myarray);
for(i=0; i<SIZE; i++)
for(j=0; j<SIZE; j++)
{
printf("%c",myarray[i][j]);
}
printf("\n");
return 0;
}
void scanA(int array[SIZE][SIZE]) // function defintion
{
int i;
int j;
for(i=0; i<SIZE; i++) // looping to scan
for(j=0; j<SIZE; j++)
{
scanf("%c\n ",&array[i][j]);
}
}**
//The scanf in the scanA function asks for 10 chars although it is looped 9 times
I want to know the reason and a solution.
Try to put a blank space before the %c (only when you read a char), cause sometimes scanf reads the enter as a char. (I'm not totally sure about it, but with me this solution works)
Sorry for my english, it isn't my main language :(

Resources