Hi,
It is my very first post, im doing it because i know some C basics and now im stuck with more advanced problems, more catchy questions.
Lets say iv allocated a memory block by function called calloc.
i have 1 bit field
struct datatype
{
unsigned short int a: 4;
unsigned short int b: 4;
unsigned short int c: 5; // 5 mitte 4 ,sest n2itek 9+9 on rohkem kui 16.
}minu;
And pointer to a bit field.
struct datatype *datatype_ptr ;
Question how to use a memmove on bit field.
(does not work at all)
//memmove((datatype_ptr ) -> b ,(datatype_ptr ) -> c, (sizeof(struct datatype)) * j2rkude_arv );
In this case (code below for whole example), is memmove function more efficient than for loop like:
for(loendur = 0; loendur != j2rkude_arv+2; loendur++) {
((datatype_ptr +loendur) -> a) = ((datatype_ptr +loendur) -> b) ;
}
Iv written Fibonacci sequence code to demo my skills to myself, school and now to you. Not much but still i hope it is worth looking at.
If you see something that i did not as good as you , let me know. Your feedback and opinions will help me alot.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define max_j2rgud 200000
#define arvutamise_korrad 100000
int skaala_yhikute_vahe = arvutamise_korrad/50;
// Globaalsed muutujad
//-----------------------------------------------------------------------------
struct datatype {
unsigned short int a: 4;
unsigned short int b: 4;
unsigned short int c: 5; // 5 mitte 4 ,sest n2itek 9+9 on rohkem kui 16.
}minu;
struct datatype *datatype_ptr ;
int j2rkude_arv = 0;
unsigned int arvutuse_nr = 0;
//-----------------------------------------------------------------------------
void liitmine()
{
short unsigned int x = 1;
short unsigned int j22k; //(datatype_ptr -> d) ? 1 bit on bitfield
(datatype_ptr -> c) = (datatype_ptr -> a) + (datatype_ptr -> b);
if ((datatype_ptr -> c) > 9) {
j22k = 1;
(datatype_ptr -> c) %= 10;
if ( x >= j2rkude_arv ) {
j2rkude_arv++;
}
}
else {
j22k = 0;
}
for(;x <= j2rkude_arv;x++){
((datatype_ptr +x) -> c) = ((datatype_ptr +x) -> a) + ((datatype_ptr +x) -> b) + j22k;
if (((datatype_ptr +x) -> c) > 9) {
j22k = 1;
((datatype_ptr +x) -> c) %= 10;
if ( x >= j2rkude_arv ) {
j2rkude_arv++;
}
}
else{
j22k = 0;
}
}
}
void fib_liigutus()
{
int loendur;
for(loendur = 0; loendur != j2rkude_arv+2; loendur++) {
((datatype_ptr +loendur) -> a) = ((datatype_ptr +loendur) -> b) ;
}
for(loendur = 0; loendur != j2rkude_arv+2; loendur++) {
((datatype_ptr +loendur) -> b) = ((datatype_ptr +loendur) -> c) ;
}
//memmove((datatype_ptr ) -> a ,(datatype_ptr ) -> b, (sizeof(struct datatype)) * j2rkude_arv );
//memmove((datatype_ptr ) -> b ,(datatype_ptr ) -> c, (sizeof(struct datatype)) * j2rkude_arv );
}
void vastuse_print()
{
int y, lugeja;
for(lugeja = 0; lugeja <= j2rkude_arv; lugeja++) {
y = j2rkude_arv - lugeja;
printf("%u",((datatype_ptr + y ) -> c));
}
printf("\n");
}
void progressi_baari_skaala()
{
printf("\n");
printf("|0----------25---------50----------75---------100%c| \n",'%'); // 50 yhitkut
}
int progressi_baar()
{
if((arvutuse_nr % skaala_yhikute_vahe) == 0) {
printf("|");
}
}
void fail_proov() // example from wiki
{
FILE *fp;
size_t count,a;
const char *str = "hello\n";
fp = fopen("sample.txt", "w");
if(fp == NULL) {
perror("failed to open sample.txt");
}
//count = fwrite(str, 1, strlen(str), fp); datatype_ptr
//count = fwrite(datatype_ptr, , j2rkude_arv, fp);
for(a = 0; a <= j2rkude_arv; a++) {
fprintf(fp,"%u",((datatype_ptr + a ) -> c));
}
printf("Kirjutas %u baiti. fclose(fp) %s.\n", a, fclose(fp) == 0 ? "6nnestus" : "ei_6nnestunud");
}
int main(int argc, char *argv)
{
unsigned int a = 0;
clock_t start, finish;
start = clock();
datatype_ptr = (struct datatype *)calloc(max_j2rgud, sizeof(struct datatype));
if (datatype_ptr == NULL) {
printf("M2lu eraldus eba6nnestus a\n");
}
(datatype_ptr) -> b = 1;
progressi_baari_skaala();
progressi_baar();
for(arvutuse_nr = 1; arvutuse_nr <= arvutamise_korrad; arvutuse_nr++) {
liitmine();
//printf("%u - ",arvutuse_nr+1);
//vastuse_print();
fib_liigutus() ;
progressi_baar();
}
finish = clock();
printf("\n");
printf("%u. Fib. nr. on %u arvj2rku pikk.\n",arvutuse_nr,j2rkude_arv+1);
printf("Arvutusteks aega kulus %f sekundit\n", ((double)finish - start) / CLOCKS_PER_SEC);
fail_proov();
free(datatype_ptr);
system("PAUSE");
return 0;
}
Sorry for long code that is not in correct language.
The code calculates about 100k fib numbers in 30 sec, you can see a 100% type progress bar in consonle. Output is last fib number in txt file.
Thank you in advance !