A function that inserts array into a binary file - arrays

My task is to write a function unesi_niz which allows the user to enter array of real numbers (maximum 100) where the entry ends with entering the number -1. The array that is entered in this way should be written to the binary file niz.bin as values of type double. The file must not contain anything other than members of the string (so it must not contain the entered number -1).
Then write a srednja_vrijednost function that calculates the mean value of the numbers in the niz.bin file and returns it. If the file does not exist or is empty, 0 should be returned.
So i started like this:
#include <stdio.h>
#include <stdlib.h>
#define vel 100
int i = 0;
int j = 0;
void unesi_niz() {
double pomocna;
double niz[100];
while (i != 100) {
scanf("%lf", &pomocna);
if (pomocna != -1) {
niz[i] = pomocna;
i++;
} else
break;
}
FILE *ulaz = fopen("niz.bin", "w");
if (!ulaz) {
printf("Greska pri otvaranju.\n"); //opening fault
}
for (j = 0; j < i; j++) {
fwrite(niz, sizeof(double), j, ulaz);
}
fclose(ulaz);
}
double srednja_vrijednost() {
double suma = 0;
if (i == 0)
return 0;
FILE *ulaz = fopen("niz.bin", "r");
if (!ulaz) {
printf("Greska pri otvaranju.\n");//opening fault
return 0;
}
double niz[100];
fread(niz, sizeof(double), i, ulaz);
int j;
for (j = 0; j < i; j++) {
suma += niz[j];
}
fclose(ulaz);
return suma / i;
}
int main() {
unesi_niz();
double n=srednja_vrijednost();
printf("%g\n", n);
return 0;
}
My code has several problems. the first is the wrong return value of the function srednja_vrijednost, when I enter the values 5 10 15, the result is 1.6667, which is nonsense, and then many "Profiler errors", my debug console says Error in line 56, main.c file: The program accesses a variable that is not initialized, however I don't see any "Forbidden Action".
Hope some of you can see what I have done wrong :)

Your code fails in unesi_niz at these lines:
for (j = 0; j < i; j++) {
fwrite(niz, sizeof(double), j, ulaz);
}
fwrite takes a pointer to the data, the size of individual elements in bytes and the number of such elements. This means your code writes j elements starting from the first element each time. You probably want to write 1 element each time. Or better yet, you want to write all i elements, since fwrite allows you to write more than one element.
fwrite(nix, sizeof(double), i, ulaz);
As an aside, your "srednja_vrijednost" logic will work, but only because you already know the size of your array in the current process (stored in i). I am not entirely sure what you are trying to do but I suspect you want to be able to read the same file back even after your process exits. For that, you would need some logic to find the size of the array. You can do this either by writing the length as well into the file, or (similar to the input) write an ending -1, or just figure out the size by calculating the file size.

Related

Changing and updating values with arrays in C

I am having issues with my c program. I am new to C programming and I have to write a program for class involving arrays. I have to use two sets of arrays and allow the user to remove a location and add a new value into that spot. I created a max array of 20 but we have to use 1 thru 5 and allow the user to remove the value in either data set. Here is what I have currently and I am getting a lot of errors saying I have ; and { in the wrong spots but it doesn't seem wrong when I go back to my text book and slides about arrays. I created constant values under myarr1 and myarr2 and SIZE is set to 20. Any and all help is appreciated I don't understand why i am getting all these errors.
int display_arr(int * count);
int remove_arr();
int myarr1[SIZE];
int main() {
printf("Data confirmation and update program written in C.\n");
int display_arr[6];
// display_arr[0]=NULLL;
int counter = 6;
my_identity();
for (i = 0, i < SIZE; i++) {
myarr1[i] = counter;
}
for (i = 0, i < SIZE; i++)
printf("Array[%d] is %d.\n", i, myarr1[i];
return EXIT_SUCCESS;
}
// deleting entry from data
int remove_arr() {
int position;
printf("Enter the location where you wish to delete element\n");
scanf("%d", & position);
}
int display_arr(int * count) {
int i;
for (i = 0; i < * count; i++)
printf("%d", myarr1[i]);
return 0;
}
//add an entry to data set
// int myarr()
}
// int input;
// printf("Enter the value you would like to add to the end of the arry:");
// scanf("%d", &input);
// if (array_select == 1){ // adds value to data set 1
// myarr1[*counter] == input;
// else
// myarr2[counter] == input; // adds value to data set 2
//return 0;
}
}
Computer programming is about being precise. This means also following the rules exactly to the point, not more, not less.
In this case, I see two things
Parenthesis/braces/brackets must be balanced:
printf("Array[%d] is %d.\n", i, myarr1[i];
Do you see the missing parenthesis at the end?
Comma and semicolon are not the same:
for (i = 0, i < SIZE; i++) {
Do you see the first comma?
For the other problems, look at the compiler error messages.

Find symmetry in Array

Hi i need to check if the array is symmetry or not. i have a function that takes in a two-dimensional array of integer numbers M and the array sizes for rows and columns as parameters, and returns 1 if M is symmetric or 0 otherwise. I tried many times but the output will be either yes to non-symmetric array or no to symmetric array
Here is my code:
#include <stdio.h>
#define SIZE 10
#define INIT_VALUE -1
int symmetry2D(int M[][SIZE], int rowSize, int colSize);
int main()
{
int M[SIZE][SIZE], i, j, result = INIT_VALUE;
int rowSize, colSize;
printf("Enter the array size (rowSize, colSize): \n");
scanf("%d %d", &rowSize, &colSize);
printf("Enter the matrix (%dx%d): \n", rowSize, colSize);
for (i = 0; i < rowSize; i++)
for (j = 0; j < colSize; j++)
scanf("%d", &M[i][j]);
result = symmetry2D(M, rowSize, colSize);
if (result == 1)
printf("symmetry2D(): No\n");
else if (result == 0)
printf("symmetry2D(): Yes\n");
else
printf("Error\n");
return 0;
}
int symmetry2D(int M[][SIZE], int rowSize, int colSize)
{
int h, k, temp;
int result;
for (h = 0; h < rowSize; h++)
{
for (k = 0; k < colSize; k++)
{
M[h][k] = M[k][h];
}
}
result = 0;
for (h = 0; h < rowSize && result; h++)
{
for (k = 0; k < colSize; k++)
{
//if it is not equal to its transpose
if (M[h][k] != M[h][k])
{
result = 1;
break;
}
}
}
if (result == 0)
{
for (h = 0; h < rowSize; h++)
{
for (k = 0; k < colSize; k++)
{
return result = 0;
}
}
}
else
return result = 1;
}
Several issues:
By your definition, a matrix is symmetric if and only if it is equal to its transpose. That can be the case only for square matrices, yet you accommodate non-square matrices as well, for no apparent reason.
Your symmetry2D() function contains serious logical flaws:
It makes the input symmetric via the loop that performs M[h][k] = M[k][h]
Even if it did not do so, it would never find the input non-symmetric, because its test for that is if (M[h][k] != M[h][k]), which must always fail.
It's unclear what you think the if/else and loop nest at the end of symmetry2D() are achieving for you, but provided that rowSize and colSize are both greater than zero, the actual effect of the whole construct is the same as a simple return result;.
It looks like the idea might have been to create an array containing the transpose of the input, and then compare the input to that. That would have worked, despite being rather grotesquely inefficient, but you never in fact create that separate array for the transpose. If you're going to test without creating the transpose -- which you should -- then
Do not modify the input array (so remove the first loop nest altogether).
Get your indexing right for the symmetry comparisons: M[h][k] != M[k][h]
For best efficiency, avoid redundant and needless comparisons. For example, if you have already tested the M[1][2] == M[2][1] then you do not need to test whether M[2][1] == M[1][2]. And you never need to test elements on the main diagonal. You could achieve this efficiency pretty easily with a better choice of loop bounds.
Also, if indeed the symmetry2D() function is supposed to avoid modifying the input array, consider declaring the element type for its first argument to be const int instead of plain int (but do not modify the type of the corresponding variable in main()). If you had written it that way in the first place then the compiler would have noticed the function's logically erroneous attempt to modify the array elements, and rejected the code.

Redirecting a file from stdin, using fgets() to read information to array

int main() {
#define MEMSIZE 100
int memory[MEMSIZE] = {0};
int i = 0;
char *temp = malloc(sizeof(100));
fgets(temp, MEMSIZE, stdin);
for (i = 0; i < (sizeof(memory)/sizeof(int)); i++) {
memory[i] = temp[i];
}
for (n = 0; n < 10; n++) { // Print contents
printf("%d - %d\n", n, memory[n]);
}
}
So today I have what seems to be a very simple question. I am taking a file from stdin, using:
./a.out < filename
My main goal is to take in the numbers provided in the file, and store them into a 1 dimensional array. My current use of fgets() works correctly, reading in line one and copying those elements into my 1D array (their ASCII values converted to int). Now, to read in my next lines, if I call fgets() again, the next line is read but it is then stored in the same place as the first values, thus overwriting them in my array in position 0-3. These values need to be stored successively in my array until EOF.
The file is in the format:
1234
5678
...
#include <stdio.h>
#define MEMSIZE 100
int main() {
int memory[MEMSIZE] = {0};
int i,n;
for (i = 0; i <MEMSIZE; i++){
if(fscanf(stdin,"%d", (memory+i))==EOF){
break;
}
}
//i is the number of ints you got
for (n = 0; n < i; n++) { // Print contents
printf("%d - %d\n", n, memory[n]);
}
return 0;
}
I dont see a reason to use dynamic allocation over here as a temp variable.
If the file is list of numbers, just read as a number, no need for fgets over here, if you still want to read it as a string, have a look at atoi func
sizeof(memory)/sizeof(int)= (sizeof(int)*MEMSIZE)/sizeof(int)= MEMSIZE
You shouldn't just loop MEMSIZE times, you need to know when it EOF
I dont know why you assumed in the printing loop that 10 is enough, i changed it to i which is number of elements
You didnt define n
I hope that i helped.

How to resort an array with file input?

I am writing a program that will ask the user for a 'n' value, they will then enter 'n' values that will be stored into an array and sorted. I have this part done easy enough.
I will compare this array with input from a number read from a text file. If the number is bigger than any of the current array values it will replace them and slide the rest down. This creates an array of the largest 'n' values
Example: n = 4 n values are : 999 972 954 462 937
a[4] = {999, 972, 954, 462, 937};
Sorted :
a[4] = {999, 972, 954, 937, 462};
if the file input is say 968 the result is.
Resorted :
a[4] = {999, 972, 968, 937, 937};
This is my current code.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if (argc<3) //error checking
return -1;
int size = atoi(argv[2]);
int a[size];
int i, j, temp=0;
printf("Enter %d numbers\n", size); //user array input for size and n values
for(i = 0; i < size; i++)
scanf("%d", &a[i]);
for(i=0; i < size; i++){ //sorting array
for(j = i+1; j <size; j++){
if( a[i] < a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
FILE *input;
input = fopen(argv[1], "r");
if(input ==NULL) //error checking
return -1;
if(fscanf(input, "%d", &temp) != 1)
return -1;
while(fscanf(input, "%d", &temp) ==1){ //loop while there is file input
for(i =1 < size; i++){ //check if temp is larger than array values
if(temp > a[i] && temp < a[i-1]){
for(j = size-1; j >= i; j--) //slide down the rest of the array
a[j] = a[j-1];
a[i] = temp;
}
}
}
for(i=0; i <size; i++){ //print out array
printf("%d ", a[i]);
}
return (0);
}
I have tried this on a smaller simpler skill were I have already created the array and the values instead of using user input. I also just passed the array check sequence through a loop that increases a number value instead of reading from a file. This seemed to work fine with something like
a[5] = {10, 8, 6, 4, 2};
number = 5; // number++ number = 6 number = 7... until 10
result: a[5] = {10, 9, 8, 7, 6};
I am sad to say that even if the program was not printing the right array at the beginning I could see there were numbers from the file. The loop is still going trough the file, but at one point the output just started being the sorted user array. I can't seem to get the array values right. Any ideas?
Continuing from my comments yesterday, I apologize if the errors were due to the retyping of your code, but that is the whole reason you want to try to cut and paste as Jonathan indicated -- eliminate human error in the transcription process.
I think I understand what your primary problem is. If your goal is to read some number of user input values from stdin, sort them in descending order, and then open a file and read a single additional value into the array in sort-order, then you must provide space for the final value in your array at the time it is declared (if using VLA's). Otherwise, you either need to create a second VLA large enough to store the values from the use and the file, and copy the user provided values to the new array or dynamically allocate the array originally (with malloc or calloc) and then realloc as needed to add space for additional values as required.
In this case, it's not that difficult since you know you are reading one value from the file. Just read the size from the command line argument and then create your array as int a[size + 1];
The rest of your task can be handled in a couple of ways. After you read (and validate) the user input, you can sort your values in descending order, read the value from the file, and create an insert & shuffle routine to insert the value in the correct order and move the remaining array elements down, or (probably a less error prone method) is simply to add the element from the file to the end of the array, and call your sort routine again.
(note: you should get used to using qsort rather than attempting to reinvent the bubble-sort, etc.. It is orders of magnitudes more efficient and much less error prone)
You need limit (or eliminate) your use of atoi as it provides zero error checking. A better approach is to use strtol and then check errno and check the end-pointer against the original to determine if there were any digits read. Below a simple helper function incorporates error-checking for strtol to insure you have an actual value for size.
Further, be careful. While you may expect the user will enter size integer values, there is no guarantee they will. It is better to track the number of values actually entered and use that value in subsequent iterations over the array rather than blindly iterating for (i = 0; i < size; i++) throughout the remainder of your code.
Whether you attempt an insert-in-place of the value read from the file, or just add it to the end of the array and call your sort routine again is up to you. I would encourage you to move your sort code into a function to provide that flexibility without having to duplicate the code in main. Look the following over and let me know if you have any questions. Since I presume this was a homework assignment, the insert-in-place case is shown below (but the simple add the file value to the end and call sort again code is included commented out)
#include <stdio.h>
#include <stdlib.h> /* for strtol */
#include <limits.h> /* for LONG_MAX/MIN */
#include <errno.h> /* for ERANGE,errno */
void sort_int_array_dec (int *a, size_t size);
long xstrtol (char *p, char **ep, int base);
int main (int argc, char **argv) {
/* read size as first argument, or 5 if none given */
int size = argc > 2 ? (int)xstrtol (argv[2], NULL, 10) : 5,
a[size + 1], /* variable length array for user + file values */
n = 0, /* number of values from user */
fval, /* value read from file */
temp, /* temporary value for array */
i = 0;
FILE *fp = NULL;
if (size < 1) return 1;
printf ("enter %d integers\n", size);
while (n < size) { /* read up to size values */
int result, c;
printf (" integer[%2d] : ", n + 1);
/* validate read of each value using scanf return */
if ((result = scanf ("%d", &temp)) != 1) {
if (result == EOF) { /* always check for EOF */
fprintf (stderr, "user canceled input.\n");
break;
}
fprintf (stderr, "error: invalid conversion.\n");
/* empty input buffer of invalid entry */
while ((c = getchar()) != '\n' && c != EOF) {}
}
else /* good value read, save, increment n */
a[n++] = temp;
}
sort_int_array_dec (a, n); /* sort a */
printf ("\nsorted array before inserting value from file:\n\n");
for (int i = 0; i < n; i++)
printf ("a[%2d]: %d\n", i, a[i]);
if (!(fp = fopen (argv[1], "r"))) {
fprintf (stderr, "error: file open failed '%s'\n", argv[1]);
return 1;
}
if (fscanf (fp, "%d", &fval) != 1) { /* read value from file */
fprintf (stderr, "error: read of file value failed.\n");
return 1;
}
printf ("\n value from file: %d\n\n", fval);
/* add fval into array in descending sort order
* (you can add it anywhere and simply call sort again, e.g.)
*/
// a[n] = fval; /* add it to the end of the array */
// sort_int_array_dec (a, n + 1); /* sort a again */
for (i = 1; i < n + 1; i++) {
if (fval > a[i-1]) {
temp = a[i-1];
a[i-1] = fval;
break; /* temp now holds value to insert at i */
}
}
if (i == n + 1) /* if already at last element just set it */
a[n] = fval;
else /* otherwise, insert and shuffle remaining elements down */
for (int j = i; j < n + 1; j++) {
int mov = a[j];
a[j] = temp;
temp = mov;
}
printf ("sorted array after inserting value from file:\n\n");
for (int i = 0; i < n + 1; i++)
printf (" a[%2d]: %d\n", i, a[i]);
return 0;
}
/** sort integer array descending (your code) */
void sort_int_array_dec (int *a, size_t size)
{
size_t i, j;
int temp;
if (size < 2) return; /* nothing to sort */
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
/** a simple strtol implementation with error checking.
* any failed conversion will cause program exit. Adjust
* response to failed conversion as required.
*/
long xstrtol (char *p, char **ep, int base)
{
errno = 0;
char *endpt = ep ? *ep : NULL;
long tmp = strtol (p, &endpt, base);
/* Check for various possible errors */
if ((errno == ERANGE && (tmp == LONG_MIN || tmp == LONG_MAX)) ||
(errno != 0 && tmp == 0)) {
perror ("strtol");
exit (EXIT_FAILURE);
}
if (endpt == p) {
fprintf (stderr, "No digits were found\n");
exit (EXIT_FAILURE);
}
if (ep) *ep = endpt;
return tmp;
}
Example Use/Output
$ cat dat/file.txt
523
$ ./bin/addintoarray dat/file.txt 4
enter 4 integers
integer[ 1] : 400
integer[ 2] : 500
integer[ 3] : 600
integer[ 4] : 700
sorted array before inserting value from file:
a[ 0]: 700
a[ 1]: 600
a[ 2]: 500
a[ 3]: 400
value from file: 523
sorted array after inserting value from file:
a[ 0]: 700
a[ 1]: 600
a[ 2]: 523
a[ 3]: 500
a[ 4]: 400
/*I'm a beginner C programmer so I don't know much of the syntax.
But I think I can help you with that problem.
I created a simple code and I hope I can really help
the integers from the file must be already sorted.
So the only integer that we will sort is the recent integer that the user inputed.
*/
/*So here's my code of sorting array of integers coming from file.
Please give it a try.
It's not the same syntax as your code but I know you can see my point*/
#include <stdio.h>
#include <stdlib.h>
//my style here is I'm declaring the max Num that I want to put in array.
//But you can do this with different style.
#define MAX_ARRAY 10
//I created separate functions
void readFile(int num_arr[]);
void sortArray(int num_arr[]);
void writeFile(int num_arr[]);
int main()
{
int num_arr[MAX_ARRAY + 1]; // take note that I added 1 (one). And you will see later why I did that
readFile(num_arr);
sortArray(num_arr);
writeFile(num_arr);
//Now we can sort them. Use a temp_num holder.
return 0;
}
void readFile(int num_arr[])
{
int x = 0;
int y = 0;
int temp_num;
FILE *sample_file_pointer = fopen("sample_file.txt", "r");
//first I read the integers from the file and put them in int array.
while(fscanf(sample_file_pointer, " %d\n", &num_arr[x]) == 1)
{
x++;
}//after reading the integers, the last element of the array we declared is still unused.. Now we will use it.
fclose(sample_file_pointer);
//now we will use the unused element by entering the 'n'. Then we will sort the array later.
printf("Enter value of n: ");
scanf(" %d", &num_arr[MAX_ARRAY]);//We put the n value in the last element of the array
}
void sortArray(int num_arr[])
{
int x = MAX_ARRAY;//We will use this to point the last element of the array.
int temp_num;
/*because the array from
the file is already
sorted, (I know you can
do the sorting of that.
That's why I didn't include
it here to make this short)
we can just test the most recent
integer that is added by the user*/
//We do that with this loop
for(int i = MAX_ARRAY; i > 0; i--)
{
if(num_arr[x] >= num_arr[i - 1])
{
temp_num = num_arr[x];
num_arr[x] = num_arr[i - 1];
num_arr[i - 1] = temp_num;
//now set the x to the swapped element to follow the recent number all through. Till the element test becomes 1.
x = i - 1;
}
}
//now we're ready to write this sorted array to a file again
}
void writeFile(int num_arr[])
{
FILE *sample_file_pointer = fopen("sample_file.txt", "w");
for(int i = 0; i < MAX_ARRAY; i++)
{
fprintf(sample_file_pointer, "%d\n", num_arr[i]);
}
//We can ignore the last element of the array. She's out of the group now. It's her fault for being the lowest.. LOL..
fclose(sample_file_pointer);
}

Deshuffle text with C

I wanted to get an opinion here. Would it be possible to write a C program that deshuffles a text file? What do I mean by that? Say I have the following data in a textfile:
1 X
4 T
3 Z
2 L
And I wanted to deshuffle it and output another file as so:
1 X
2 L
3 Z
4 T
Such that all of the data following the number is preserved with the actual index number. Do know that I have 40,500 shuffled entities, so that should probably be taken into account since that could take a long time if the program needs to loop through all of the entities for each entity...And I only used letters for representation. The actual data files don't have letters, but rather have floats. Sorry if this causes any confusion
So, bottom line, would this be possible with C? And if so, could I get a hint at where to start? I could obviously input all of the textfile data into an array dat[][], but how should I deshuffle it then?
Thanks!
Amit
Look up qsort, its part of stdlib.
I'd just use sort -n instead of writing a program.
If you know the largest index in the file and there are no holes (i.e. all indices are present), you can create an array of that size, then as you read each line of the file, put the data into the correct location in the array. When you're done reading the file, your array will have all the elements in the correct order.
You can build a vector< pair<int,float[4]> > and just sort it using STL.
Just noticed a c tag.... But C idea is about the same:
Build an array with the data:
struct pair
{
int idx;
float val[4];
}
pair *data;
And then just sort it.
For those interested, I ended up using a series of loops to write this program, the code is show below. The only downside of it is that if my nrow variable is equal to 40,500, this program can take as long as 30 mins to run on a 3GHz dual core computer. I'm sure there are ways to optimize it, though at least it does what I want...for now.
Here's the code:
#include "stdlib.h"
#include "stdio.h"
// Deshuffle the output files of the bond/swap mode
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("\ndeshuffle usage: [number of chains] [number of atoms] [.dat file] \n");
exit(1);
}
int i,j,k,l;
int nch = atoi( argv[1] );
int ns = atoi( argv[2] );
double **in_dat, **s_dat, dm1;
int nrow = ns*nch;
in_dat = (double**) calloc(nrow, sizeof(double*));
s_dat = (double**) calloc(nrow, sizeof(double*));
for (i=0; i<nrow; i++) {
in_dat[i] = (double*) calloc(6, sizeof(double));
s_dat[i] = (double*) calloc(6, sizeof(double));
for (j=0; j<6; j++)
in_dat[i][j] = 0.0;
s_dat[i][j] = 0.0;
}
// store input data into 2D array in_dat
FILE *inp;
inp = fopen( argv[3], "r" );
for (i=0; i<nrow; i++) {
for (j=0; j<6; j++) {
fscanf(inp, "%lf", &dm1);
in_dat[i][j] = dm1;
}
}
fclose(inp);
// Sort data in s_dat based on comparison with in_dat
k=0;
while (k < (nrow)) {
for (i=0; i<nrow; i++) {
for (l=0; l<nrow; l++) {
if (in_dat[l][0] == (k+1)) {
for (j=0; j<6; j++) {
s_dat[i][j] = in_dat[l][j];
}
k++;
break;
}
}
}
}
// Write sorted data to file
FILE *otp;
otp = fopen("results.out", "w");
for (i=0; i<nrow; i++) {
for (j=0; j<6; j++) {
fprintf(otp, "%lf \t", s_dat[i][j]);
if (j==5)
fprintf(otp, "\n");
}
}
fclose(otp);
printf("\n Done. \n\n");
return 0;
}

Resources