efficient way to get numbers from user and fill Matrix - c

I want to get numbers from user in a single line for example:
2 1 2 3 4
The first number: 2 means that my Matrix should be with size of 2x2 and the next 4 numbers should insert into my Matrix (Matrix dimension should be n²).
Currently I have this:
int dimension, num;
int *mat;
int numcounter = 0;
int i = 0;
int j, k, t;
char temp;
printf("Please enter numbers: ");
do {
scanf("%d%c", &num, &temp);
i++;
if (i == 1)
{
/* Set Matrix dimension. */
dimension = num;
if (dimension < 2)
{
printf("Size must be a valid number");
return 1;
}
else
/* Allocate dimension size. */
mat = (int*)malloc(dimension * dimension * sizeof(int*));
}
else
{
/* Fill Matrix. */
}
} while (temp != '\n' || temp == EOF)
So here I have all the number and now I need to fill my Matrix but instead of put all the numbers inside temp array and then fill my Matrix I wonder how to do that without another memory allocation.

You can do it simply using a VLA. The OP's initial question never mentioned that OP needs to get the input in a line and parse it. I have given that as an answer in the second part. But simply that is not needed. It is not a feasible reason that you have to get the numbers all at once. You can get the size using one scanf and then the elements in another scanf.
scanf("%d", &num);
//Then you know the dimension of the array.
int arr[num][num];
for(size_t i=0;i<num; i++)
for(size_t j =0; j< num; j++)
if( scanf("%d",&arr[i][j]) != 1)
{
fprintf(stderr,"Error in input");
exit(1);
}
Also as you know how many numbers will be read you don't need to continue scanning until you get '\n' or EOF.
Given your case your over complicating things IMHO. As you know the numbers you can get the benefit of VLA.
If you have to get all the numbers in a single line you need to look at 3 functions. fgets() and strtol. Those will help you achieving reading everything in a line and then tokenizing.
To give you a demo of what I said you can do this
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 256
int main(void)
{
char line[BUFSIZE];
long temp[BUFSIZE];
int i = 0;
if (fgets(line, sizeof line, stdin) != NULL)
{
const char *ptr = line;
while (*ptr != '\0')
{
char *endptr = NULL;
long val = strtol(ptr, &endptr, 10);
if (endptr != ptr)
temp[i++] = val;
else
break;
ptr = endptr;
}
}
int sz = temp[0];
if( sz <= 0 )
{
fprintf(stderr,"Error in size input");
exit(1);
}
int tempIn = 1;
long arr[sz][sz];
for(size_t i = 0; i < sz; i++)
for(size_t j = 0; j < sz; j++)
arr[i][j]= temp[tempIn++];
for(size_t i = 0; i < sz; i++)
for(size_t j = 0; j < sz; j++)
printf("%ld ",arr[i][j]);
}
In the second code, as you can see fgets has been used which basically read a line and then strtol has been used. As you have mentioned that you will give a single line input of numbers.
Now what we did?
The line scanned and the parsed number by number using strtol. For a brief overview of strtol check this.
Also OP asked for how to use dynamic memory allocation to do the same thing. And there is no way a while loop is needed here. This is redundant. So while modifying I will add the code that will be able to do it much more simpler way.
scanf("%d", &num);
int matIndex = 0;
/* Set Matrix dimension. */
dimension = num;
if (dimension <= 0)
{
printf("Size must be posiitve integer");
return 1;
}
else
{
mat = malloc(dimension * dimension * sizeof *mat);
if ( mat == NULL ){
fprintf(stderr, "%s\n", "Error in malloc");
exit(1);
}
}
// All the numbers will be taken as elements of the dynamically allocated array,
for(size_t i = 0; i < dimension*dimension ; i++)
if( scanf("%d",&mat[i]) == 1){
//ok
}
else{
fprintf(stderr,"Some error occured");
break;
}
When you want to access the i-th row and j-th column element, you can do this mat[i*dimension+j] //equivalent to mat[i][j]
Some useful information:-
Allocating dim*dim memory doesn't let you access the element in the form of mat[i[[j]. Because you allocated a single chunk of memory - you have to calculate the positions explicitly and access the element in the linear array.
You can do all the scanning using scanf() but you need to be careful when having scanf(). But yes you can get this thing done using scanf also.
Also there is another thing to know about. The VLA will have automatic storage duration and limiting the memory you can have use this way. The dynamically allocated memory has much higher limit giving you a scope of having much larger dimensions array.
Note that in dynamic memory allocation you will need to free the allocated memory when you are done working with it.
If you want to use mat[i][j] not mat[i*dim+j] you can consider creating one jagged array.
Few things that you will need:
Reading Suggestion:
How to debug small programs
Beginner's guide away from scanf
Book list

You can use following code:
Note: You can redirect STDIN to your file. See posts available here
#include <stdio.h>
#include <stdlib.h>
int main()
{
int D, i, j;
freopen("testfile.txt","r",stdin);
printf("Ender dimention of matrix: ");
scanf("%d",&D);
int** matrix = malloc(D*D*sizeof(int));
for(i=0; i<D; i++)
{
for(j=0; j<D; j++)
{
printf("Ender element of matrix: ");
scanf("%d",&matrix[i][j]);
}
}
printf("Matrix is: \n");
for(i=0; i<D; i++)
{
for(j=0; j<D; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
free(matrix);
return 0;
}

Related

printing 2 characters and keeping them through dynamically allocated 2d array

Hello I am trying to print something like this with 2d array.
Note that when user enters the same number, character should be printed above existing char.
EXPECTED RESULTS:
Input 1: 3 //user1 inputs 3
****
****
**x*
Input 2: 1 //user2 inputs 1
****
****
y*x*
Input 3: 1 //user1 inputs 1
****
x***
y*x*
current results:
enter first: 3
3***
***
**x
enter second: 1
1******
******
xx****
enter first: 2
2*********
*********
***xxx***
But keeping printed values on its previous places.
The problem is that they don't get printed in right order. And also it seems that I haven't done the best job with 2d array which is dynamically allocated.
Here is something what I've tried:
#include <stdio.h>
#include <stdlib.h>
int num(int term)
{
int number1;
int number2;
if(term==1)
{
scanf("%d", &number1);
return number1;
}
if (term==2)
{
scanf("%d", &number2);
return number2;
}
return 0;
}
void function(int a, int b, int result[], int size)
{
int i = 0;
int j = 0;
int desired_num = 0;
int count = 0;
int *arr[a];
for (i = 0; i < a; i++)
arr[i] = (int *)malloc(a * sizeof(int));
for (i = 0; i < a; i++)
for (j = 0; j < b; j++)
arr[i][j] = ++count;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (int counter = 0; counter < size; counter++)
{
if (arr[i][j] == arr[a - 1][result[counter] - 1])
{
arr[i][j] = desired_num;
}
if (arr[i][j] == desired_num)
{
printf("%s", "x");
}
else
{
printf("*");
}
}
}
printf("\n");
}
}
int main()
{
int counter = 1;
int i = 0;
int given_number;
int array[20];
for (;;)
{
if (counter % 2 != 0)
{
printf("enter first: ");
given_number = num(1);
printf("%d", given_number);
}
else
{
printf("enter second: ");
given_number = num(2);
printf("%d", given_number);
}
array[i] = given_number;
function(3, 3, array, counter);
counter++;
}
return 0;
}
array[i] = given_number;
i is never changed from the value of 0. You are only ever overwriting the first element of array each iteration. The other 19 elements remain in an indeterminate state.
counter and array are passed to function, as size and result respectively:
This means as size is incremented, it is used as a bounds for accessing elements of result; elements that contain indeterminate values.
for (int counter = 0; counter < size; counter++)
{
if (arr[i][j] == arr[a - 1][result[counter] - 1])
This will surely lead to Undefined Behaviour as those indeterminate values are used to index arr, effectively accessing random memory offsets. This fact alone makes it hard to reason about the output you are seeing, as really anything is valid.
While perfectly valid, the variable-length array of dynamic allocations is a somewhat perplexing choice, especially considering you fail to free the memory allocated by malloc when you are done with it.
int *arr[a];
for (i = 0; i < a; i++)
arr[i] = (int *)malloc(a * sizeof(int));
int arr[a][b]; would work, given a and b are not stack-crushingly large values (or non-positive). You are, or would be, bounded by the size of array in main anyway.
The triply nested loop is confused at best. There is only logic for printing the x and * characters, so you obviously will never see a y.
For each element of arr, you iterate through each element of result. If the current element of arr equals the value of the column selected by the current value of result ([result[counter] - 1]) in the last row (arr[a - 1]) you print x, otherwise *.
Again UB from utilizing indeterminate values of result, but you can see you are printing a * b * size characters, plus newlines, each iteration.
This is severely flawed.
Some other things of note:
The two branches of the if .. else statement in the num function do the exact same thing, just with different identifiers.
The two branches of the if .. else statement in main are identical, other than the first printf in each, and the integer value passed to num, which have the same effect.
This means the only thing that needs to branch is the printf argument.
A generic function for getting an integer would work fine
int get_num(void)
{
int n;
if (1 != scanf("%d", &n)) {
fprintf(stderr, "Could not read input.\n");
exit(EXIT_FAILURE);
}
return n;
}
for use inside main
if (counter % 2 == 0)
printf("enter first: ");
else
printf("enter second: ");
given_number = get_num();
A small issue: printf("%d", given_number); is muddling the output slightly.
There is no reason to repeatedly generate the array. Initialize an array in main to serve as the state of the program. Over time, fill it with the users' selections, and simply print the array each iteration.
Make sure to always check the return value of scanf is the expected number of conversions, and ensure the integers provided by the users will not access invalid memory.
Here is a cursory example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EMPTY '*'
#define PLAYER_ONE 'X'
#define PLAYER_TWO 'O'
int get_num(void)
{
int n;
if (1 != scanf("%d", &n)) {
fprintf(stderr, "Could not read input.\n");
exit(EXIT_FAILURE);
}
return n;
}
int main(void)
{
const size_t rows = 6;
const size_t cols = 7;
char board[rows][cols + 1];
memset(board, EMPTY, sizeof board);
/* our rows are now strings */
for (size_t i = 0; i < rows; i++) {
board[i][cols] = '\0';
puts(board[i]);
}
unsigned char turn = 1;
while (1) {
printf("Player %s, Enter column #(1-%zu): ",
turn & 1 ? "One" : "Two", rows);
int input = get_num();
if (1 > input || input > cols) {
printf("Invalid column [%d]. Try again...\n", input);
continue;
}
size_t sel = input - 1;
if (board[0][sel] != EMPTY) {
printf("Column [%d] is full! Try again...\n", input);
continue;
}
size_t n = rows;
while (n--) {
if (board[n][sel] == EMPTY) {
board[n][sel] = turn & 1 ? PLAYER_ONE : PLAYER_TWO;
break;
}
}
for (size_t i = 0; i < rows; i++)
puts(board[i]);
turn ^= 1
}
}

Sort numbers from a .txt file in C

I made a sorting algorithm that works alright when the numbers I need to sort are in an array inside the code, like this:
int sort[] = {16,8,23,4,42,15};
But I need the code to sort the numbers from a .txt file, I do know the size of the file (so no need of a sizeof to know how many numbers you need to sort) but the problem is that the numbers in the file are not separated by commas, only spaces, and I don't know how to make my code operate with this list of numbers.
My code is like this, and like I said, it works when the sorting array of numbers is inside the code separated by comas:
int main(){
int temp, size;
int sort[] = {16,8,23,4,42,15};
size = sizeof(sort) / sizeof(int);
for(int j = 0; j < size; j++){
for(int i = 0; i < size; i++){
if(sort[i] > sort[i+1]){
temp = sort[i];
sort[i] = sort[i+1];
sort[i+1] = temp;
}
}
}
for(int p = 0; p < size; p++){
printf("%d ", sort[p]);
}
}
And I also know that to open a file in C the code is something like this:
FILE* f;
f = fopen("1000.txt", "r");
if(f == 0){
printf("Database unavaible or corrupted\n\n");
exit(1);
}
But I don't know what do next, how do I get this file with unsorted numbers not separated by commas and make my code sort and print them?
You can use fscanf to read the number from file then store these numbers in an array. Finally, you can using your sorting function to sort the this array.
The code below is an example for reading the numbers from the file:
#include <stdio.h>
#define MAX_NUM 10
int main() {
FILE * fp = fopen("input.txt", "r");
if(!fp) {return -1;}
int array[MAX_NUM] = {0}; // you can use array[size] if you know exactly how many numbers in the file
int i = 0;
while(i < MAX_NUM && fscanf(fp, "%d", &array[i]) == 1) {
printf("a[%d] = %d\n", i, array[i]);
i++;
}
// sort the array here as you did in your code
return 0;
}
OT, your code has a mistake in for loop:
for(int i = 0; i < size; i++){
if(sort[i] > sort[i+1]){...}
...
}
when i = size - 1, sort[i+1] will become sort[size] that is out of the array sort, because the maximum index that you can access is size-1 not size. It should change to:
for(int j = 0; j < size; j++) {
for(int i = 0; i < size-j-1; i++) {...}
}
To read the numbers you can use fscanf but you still have the problem of memory allocation.
Imagine the file has 20 elements but the MAX_NUM is 4096, you would be taking 4086*4 bytes of memory unnecessarily.
On the other hand the file can have 8000 elements but you can only store 4096.
What you can do is dynamic memory allocation.
int *array = (int*)malloc(10*sizeof(int));
unsigned numbers_counter = 0;
while(fscanf(f,"%i",&array[numbers_counter++])==1){
if(numbers_counter>=sizeof(array)){
if((array = (int*) realloc(array,sizeof(int)*sizeof(int)*10)) == NULL)
exit(1);
}
}
What I do in this segment is allocate a space of 10 int on memory, then, when the array is full (if(numbers_counter>=sizeof(array))) the array for a bigger set of numbers.
If you have until 10 numbers, it only occupies 10*4 bytes, the from 11 to 100 it occupies 100*4, etc.

Allocating memory to 2D array using an array of NULL (c)

thanks for taking the time in reading this.
In my question a "vector" is defined as a 1D dimensional array of integers.
Therefore an array of vectors would be a 2D dimensional array in which every vector can be of a different length.
I'm asked to use:
int** vectors- the 2D array
int size -an integer that represents how many vectors exist inside **vectors
int* sizes-a 1D array of integers that represents the length of the vectors
for example,for:
vectors = {{4,3,4,3},{11,22,33,44,55,66},NULL,{5},{3,33,333,33,3}}.
size is 5 (there are 5 vectors inside vectors).
sizes is {4,6,0,1,5} (4 is the length of the first vector and so on).
size is inputted by the user at the beginning of main() and **vectors&*sizes are dynimacilly allocated with size's value.
I'm asked to write the function:
int init(int ***vectors, int **sizes, int size) which initializes **vectors to be an array of NULLs and *sizes to be an array of zeros.
I came up with this code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
int i, k,j;
printf("check\n");
*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
return 0;
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
printf("check 2\n");
for (k = 0; k<size; k++)
{
if (*(vectors+k) != NULL)
printf("didn't work\n");
else
printf("current is null\n");
}
*sizes= (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
return 0;
for (j= 0; j < size; j++)
{
*(sizes + j) = 0;
printf("%d ", *(sizes + j));
}
printf("\n");
return 1;
}
int main()
{
int size, i;
int** vectors = NULL;
int* sizes = NULL;
printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
printf("%d\n", init(&vectors, &sizes, size));
printf("size is %d now\n", size);
// for (i = 0; i < size; i++)
// printf("%d ", *(sizes+i));
printf("check 3\n");
free(sizes);
free(vectors);
printf("check 4\n");
printf("check 5\n");
return 0;
}
forgot to mention that init returns 0 if it fails to allocate memory and 1 otherwise.
printing the "checks" was so I could see where the program fails.
the problem is that no matter what,after printing the last check (check 5)
the program fails.(Run-Time Check Failure #2)
if anyone could help me understand what I'm doing wrong I would HIGHLY appreciate it.
thanks alot for reading and have an amazing day.
edit:
i also printed the array sizes/vectors inside init just to see if it prints zeros/nulls,i don't actually need to do it.
One problem of OP's code is in the pointer arithmetic. Given:
int ***vectors;
*vectors = malloc(size * sizeof(int*));
This loop:
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
Would iterate over the next unallocated pointer to pointer to pointer to int, while what the OP needs is
for (i = 0; i < size; i++)
{
*(*vectors + i) = NULL; // or (*vectors)[i] = NULL;
}
The same holds in the following loops, where *(sizes + j) is used instead of *(*sizes + j) (or (*sizes)[j]).

Dynamic string matrix in C

I am having the next problem. I'm trying to create a dynamic matrix of strings which dimensions are defined by the user. The next code I made returns a segmentation fault:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char ***pa = NULL;
int rows, columns , max, i, j = 0;
//Ask dimension
puts("Type number of columns: ");
scanf("%d",&columns);
puts("Type number of rows: ");
scanf("%d",&rows);
puts("Type max string length: ");
scanf("%d",&max);
//Allocate in memory
***pa = (char ***)malloc(rows * sizeof(char**));
for(i=0;i<rows; i++){
pa[i] = (char **)malloc(columns * sizeof(char*));
for(j=0; i<columns;j++){
pa[i][j] = (char)malloc((max+1)*sizeof(char));
}
}
puts("Memory OK");
//Fill the matrix
i = 0;
j = 0;
for(i=0;i<rows;i++){
strncpy(pa[i][j] , "Hello world", max);
for(j=0;j<columns;j++){
strncpy(pa[i][j] , "Hello world", max);
}
}
//Shows the matrix
for(i=0;i<rows;i++){
puts(pa[i][j]);
for(j=0;j<columns;j++){
puts(pa[i][j]);
}
}
//Cleans the matrix
free(pa);
return 0;
}
I know I should check if the returned pointer by malloc is NULL but I didn't included it yet.
The program does not print this so the problem is in the malloc for's
puts("Memory OK");
How can I debug this so I could know what happend? Did I made a mistake while coding it?
I'd recommend you leverage c99 to the fullest, and allocate the entire matrix as a single block. You'll be reducing the fragmentation of your programs memory and making iteration over the matrix more cache friendly:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int rows, columns , max;
puts("Type number of columns: ");
scanf("%d",&columns);
puts("Type number of rows: ");
scanf("%d",&rows);
puts("Type max string length: ");
scanf("%d",&max);
char (*mat)[columns][max+1] = malloc(rows * columns * (max + 1));
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j) {
strncpy(mat[i][j] , "Hello world", max);
}
}
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
puts(mat[i][j]);
}
}
free(mat);
return 0;
}
Other added bonii:
Removes the complexity involved with allocating your memory. No more nested loops with mallocs
No need to loop for freeing the whole thing, just free the one pointer. You already do that, but in your case it leaks memory.
Cons:
May fail to allocate the entire block for large matrices. Here the fragmented approach may be better.
Change
pa[i][j] = (char)malloc((max+1)*sizeof(char));
into:
pa[i][j] = (char *)malloc((max+1)*sizeof(char));
Also, no need for the outer (first) strncpy in the main.
[EDIT]
Also, the first allocation line ***pa = ... should be pa = .... You want to assign an address to the pointer pa, not de-reference it (***pa de-references the pointer pa that has not been yet initialized).
Thanks to veryone for helping me to solve it. I've learned new things with you.
First of all, I changed this.
pa = malloc(rows * sizeof(char**));
for(i=0;i<rows; i++){
pa[i] = malloc(columns * sizeof(char*));
for(j=0; j<columns;j++){//Here there was an i. That was totally wrong.
pa[i][j] = malloc((max+1)*sizeof(char));
}
}
Now it works and allocates correctly the matrix into de memory.

using realloc with a for loop

I am using realloc to allocated memory at runtime in dynamic array. Firstly, I allocated a memory with calloc with sizeof a random integer a. In my program, I have taken a=2. After that I want to store some 14 random values generated, so I have to resize the memory using realloc. I am doing the same in a for loop. FOr 1 iteration, realloc works but after that size doesnt increase and a error occurs "corruption in heap". I am not able to understand the problem. Pls help me if you can, in understanding where the problem is occuring and how to solve it.
Thanks a lot.
Below is my code:
j=j*a; //a=3
numbers = (int*) calloc(b, j); //b=14, no of elements I want to store
printf("Address:%p\n",numbers);
if (numbers == NULL)
{
printf("No Memory Allocated\n");
}
else
{
printf("Initial array size: %d elements\n", a);
printf("Adding %d elements\n", b);
}
srand( (unsigned) time( NULL ) );
for(count = 1; count <= b ; count++)
{
if(i <= j)
{
numbers[count] = rand() % 100 + 1;
printf( "Adding Value:%3d Address%p\n", numbers[count],numbers[count] );
i++;
}
if (i > j)
{
printf("Increasing array size from %d bytes to %d bytes\n",j,j*a);
j=j*a;
numbers = (int*) realloc(numbers,j);
printf("Address:%p\n",numbers);
if(numbers == NULL)
{
printf("No Memory allocated\n");
}
}
}
free(numbers);
return 0;
}
The initial array length (length and size are not the same) is b, not a.
Adding b elements? I don't think you are.
Arrays are zero-based in C. You loop should be for(count=0; count<b ; count++).
count is a terrible name for a loop variable. count should hold the number of elements and not be a loop variable.
It's hard to imagine what j could be. Since you use it as the element size in your call to calloc it ought be at least be a multiple of 4, the size of in int. What is it?!
The realloc doesn't seem to bear any relation to the calloc.
I'm sure there are lots of other problems. If you want more help then a clear statement of what your goal is would be required.
EDIT
It sounds like you want something like this:
int capacity = 10;
int count = 40;
int i;
int* array = (int*)malloc(capacity*sizeof(int));
for (i=0; i<count; i++)
{
if (i==capacity)
{
capacity *= 2;
array = (int*)realloc(array, capacity*sizeof(int));
}
array[i] = RandomIntInRange(1, 100);
}
free(array);
Notes:
No error checking. In production code you would check that the allocations succeeded, and the realloc done this way would leak if it failed. But there's no point confusing the message with error checking when you are still at this level of understanding.
No reading input - you can do that.
No writing output - you can do that.
The integer "j" is not initialized in your code, resulting in a = 0 * 3, meaning a will be zero and no memory will be allocated. The segfault is due to you not handling that numbers is NULL. Change to and set j to something meaningful
#include <stdlib.h>
#include <stdio.h>
void
main (int argc, char *argv[])
{
int a = 3;
int j = 1 * a; //a=3
int b = 14;
int *numbers = calloc (b, j); //b=14, no of elements I want to store
int count = 0, i = 0;
printf ("Address:%p\n", numbers);
if (numbers == NULL)
{
printf ("No Memory Allocated\n");
return;
}
else
{
printf ("Initial array size: %d elements\n", a);
printf ("Adding %d elements\n", b);
}
srand ((unsigned) time (NULL));
for (count = 1; count <= b; count++)
{
if (i <= j)
{
numbers[count] = rand () % 100 + 1;
printf ("Adding Value:%3d Address%p\n", numbers[count],
&(numbers[count]));
i++;
}
if (i > j)
{
printf ("Increasing array size from %d bytes to %d bytes\n", j,
j * a);
j = j * a;
numbers = (int *) realloc (numbers, j);
printf ("Address:%p\n", numbers);
if (numbers == NULL)
{
printf ("No Memory allocated\n");
}
}
}
free (numbers);
}

Resources