C, clock function return only 0.00000 - c

When i run this code, it print everytime 0.000000 instead of something plausible:
#include <stdio.h>
#include <time.h>
#define n 10
void ordinaVett(int v[], int elem){
clock_t inizio, fine;
double tempo;
int i, j, x, posmin, tmp;
printf("\n\nOrdinamento vettore di %d elementi per %d volte: \n", elem, n);
printf("Sele-sort:\n");
for(x = 0; x < n; x++){
inizio = clock();
//Ordinamento con Sele-sort
for(i = 0; i < elem - 1; i++){
posmin = i;
for(j = i + 1; j < elem; j++){
if(v[j] < v[posmin])
posmin = j;
}
if(posmin != i){
tmp = v[i];
v[i] = v[posmin];
v[posmin] = tmp;
}
}
fine = clock();
//Visualizzazione vettore
for(i = 0; i < elem; i++){
printf("%d |", v[i]);
}
tempo = (double)(fine - inizio)/CLOCKS_PER_SEC;
printf(" Tempo: %f\n", tempo);
}
}
int main(){
srand(time(NULL));
int elementi = 20, i;
int v[elementi];
printf("Creazione vettore 20 elementi: \n");
for(i = 0; i < elementi; i++)
{
v[i] = rand() % 1001;
printf("%d |", v[i]);
}
ordinaVett(v, elementi);
}
What should I do for be able to see a normal output?
Thanks!
some words are in Italian so: tempo is time, ordinaVett is sortArray, inizio is start and fine is end.

Ok nevermind, I realized that a CPU is too fast and a double variable couldn't hold a so simple number. This's for sure a pretty dumb exercize to put on a school book.

Related

error Process returned -1073741571 (0xC00000FD) in c

I am trying to order a very large array by the bubble method (to take execution times) and after 40000 data it freezes. If someone could tell me what the error is. Thank you
the data is taken from a text file previously created with 10 million jumbled data
The code is the following:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubbleSort(int *arr, int n){
int i, temp;
for(i = 0; i < n-1; i++){
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
if(n == 1){
return;
}
else{
bubbleSort(arr, n-1);
}
}
int main(){
double time_spent = 0.0;
int n, i, num, *Aleatorio, cont = 0;
FILE *f = fopen("archivo.txt","rt");
printf("Ingrese la cantidad de datos a analizar: ");
scanf("%d", &n);
Aleatorio = (int*)malloc(n*sizeof(int));
if(Aleatorio == NULL){
printf("AAAAAAAAAAAAAAAAAAAA");
}
else{
for(i = 0; i < n; i++){
fscanf(f, "%d", &num);
Aleatorio[i] = num;
}
}
fclose(f);
/*
printf("\n\nArray Desordenado : \n");
for(i = 0; i < n; i++){
printf("%d ",Aleatorio[i]);
//cont++;
}
*/
clock_t begin = clock();
bubbleSort(Aleatorio, n);
clock_t end = clock();
free(Aleatorio);
printf("\n\n\n\n\n\n\nArray Ordenado : \n");
for(i = 0; i < n; i++){
printf("%d ",Aleatorio[i]);
cont++;
}
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n cont = %d", cont);
printf("\n\nThe elapsed time is %f seconds", time_spent);
return 0;
}
I have seen that it is a stackoverflow error, but I have not found what the error could be since a certain amount of data is ordered by me.
the code has to sort up to 10 million pieces of data (I know it will take forever but I have to do it)

changing program function from fields to structure

is there any way I convert this program to using structures? I would like to do that but I have no idea how, but I know its possible. I am just begginer in this soI will be thankful for any kind of help. Thank you
#include <stdio.h>
int main(void)
{
int pole[100];
int x, i, j, q, min[100], max[100], min_cislo, max_cislo, min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
min_cislo = pole[i];
max_cislo = pole[i];
min[min_pocet++] = i;
max[max_pocet++] = i;
continue;
}
if(pole[i] <= min_cislo){
if(pole[i] == min_cislo){
min[min_pocet++] = i;
}else{
min_pocet = 0;
min_cislo = pole[i];
min[min_pocet++] = i;
}
}
if(pole[i] >= max_cislo){
if(pole[i] == max_cislo){
max[max_pocet++] = i;
}else{
max_pocet = 0;
max_cislo = pole[i];
max[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", min_cislo);
for(j = 0; j < min_pocet; j++){
printf(" %d,", min[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", max_cislo);
for(q = 0; q < max_pocet; q++){
printf(" %d,", max[q]);
}
printf("\n");
return 0;
}
Okay I figured it your like this because the quest was to save value and position of numbers into struct. see my code:
#include <stdio.h>
struct {
int min_hodnota;
int max_hodnota;
int min_pozicia[100];
int max_pozicia[100];
} bod;
int main(void)
{
int pole[100];
int x, i, j, q , min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
bod.min_hodnota = pole[i];
bod.max_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
bod.max_pozicia[max_pocet++] = i;
continue;
}
if(pole[i] <= bod.min_hodnota){
if(pole[i] == bod.min_hodnota){
bod.min_pozicia[min_pocet++] = i;
}else{
min_pocet = 0;
bod.min_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
}
}
if(pole[i] >= bod.max_hodnota){
if(pole[i] == bod.max_hodnota){
bod.max_pozicia[max_pocet++] = i;
}else{
max_pocet = 0;
bod.max_hodnota = pole[i];
bod.max_pozicia[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", bod.min_hodnota);
for(j = 0; j < min_pocet; j++){
printf(" %d,", bod.min_pozicia[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", bod.max_hodnota);
for(q = 0; q < max_pocet; q++){
printf(" %d,", bod.max_pozicia[q]);
}
printf("\n");
return 0;
}
I guess you want to build a struct whose fields are the bunch of variables you have defined at the beginning. Well, as pointed by #"L. Scott Johnson", you don't have to. But if you want anyways you jus have to define a struct like this:
struct {
int x;
int i;
int j;
int min[100];
}example;
and just use the struct like this:
example.i=4;
example.min={1,4,5,3,4,8,99,123... };
etc.

Get the average of temperature of cities

The program is about finding the average temperature of all cities, but the result doesn't make sense (it is wrong)
I have tried splitting the program into functions for better understanding, but that didn't help me much.
#define amount_cities 5
#define amount_temp 3 // temp = temperature
int mintemp(int mas[amount_cities][amount_temp], int *index);
int maxtemp(int mas[amount_cities][amount_temp], int *index);
float avgtemp(int mas[amount_cities][amount_temp]);
int main(){
int arr_temp[amount_cities][amount_temp];
int i, j, ind;
// Set a temperatures for each city
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %d \n", i);
for(j = 0; j < amount_temp; ++j){
printf("Temperature %d - ", j+1);
scanf("%d", &arr_temp[i][j]);
}
}
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %ds \n", i);
for(j = 0; j < 3; ++j){
printf(" %d.- %d", j+1, arr_temp[i][j]);
}
printf("\n");
}
printf("Minimal temperature = %d ", mintemp(arr_temp, &ind));
printf("for city %d \n", ind);
printf("Maximal temperature = %d ", maxtemp(arr_temp, &ind));
printf("City %d \n", ind);
printf("Average temperature = %.2f\n", avgtemp(arr_temp));
return 0;
}
int mintemp(int mas[amount_cities][amount_temp], int *index){
int m, n, min_t;
min_t = mas[0][0];
*index = 0;
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] < min_t){
min_t = mas[m][n];
*index = m;
}
}
}
return min_t;
}
int maxtemp(int mas[amount_cities][amount_temp], int *index){
int m, n, max_t;
max_t = mas[0][0];
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] > max_t){
max_t = mas[m][n];
*index = m;
}
}
}
return max_t;
}
float avgtemp(int mas[amount_cities][amount_temp]){
int m, n;
float average_t = 0.0; // Average temperature
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
average_t += mas[m][n];
}
}
average_t /= amount_cities*mas[m][n];
return average_t;
}
I expected the output to be , 33.00 but got Average temperature = 30.00 instead.

Segmentation fault core dumped 2d array

I'm trying to compile a c code under linux using gcc-4.9 (tried also 5.4) while so, I faced a segmentation fault error.
Program received signal SIGSEGV, Segmentation fault.
0x080492e6 in dot (p1=0x8d9e6c0 <permy>, p2=0x3d77ca7c) at autrq.h:135
135 j = p2[i];
this is the part of code where the problem exists:
#define N 239
#define K 120
void dot(int p1[N], int p2[N]) {
int p3[N], i, j; //printf("\n debut dot ");
for (i = 0; i < N; i++){
p3[i] = p1[i];
}
for (i = 0; i < N; i++) {
j = p2[i];
if(j>=N){
printf("Too large\n");
}
else{
p1[i]=p3[j];
}
} //printf("\n fin dot ");
}
void GenAut(int permy[N]) {
int i, j, c, f;
//printf("\n debut GenAUT ");
int inf[K], mo[N], mi[N];
for (i = 0; i < N; i++){
permy[i] = i;
}
j = GenIdex(1, 100);
for (c = 0; c < j; c++) {
f = GenIdex(0, pos);
//printf("\n pos: %d et f %d ",pos,f);
dot(permy, automorf[f]);
}
}
automorf is an array n x n long (int automorf[n][n])
Can you please help correcting the problem?
Try this and see output
we have not p3[3]; and trying to access make error or undefined behavior.
#include <stdio.h>
#define N 3
void dot(int p1[N], int p2[N]) {
int p3[N], i, j; //printf("\n debut dot ");
for (i = 0; i < N; i++){
p3[i] = p1[i];
}
for (i = 0; i < N; i++) {
printf("i:%d p3[i]%d\n", i,p3[i]) ;
}
printf("\n") ;
for (i = 0; i < N; i++) {
j = p2[i];
printf("i:%d j:%d p3[%d]:%d\n", i, j, j, p3[j]) ;
p1[i]=p3[j];
}
//printf("\n fin dot ");
}
int
main(){
int x[N]={10,20,30};
int y[N]={1,2,3};
dot(x, y);
printf("\n");
return 0;
}

I need to pass an array of structures to a function [large code]

I've been working on this for days but can't seem to make it work out.
Sorry in advance for the unholy length of this, so if anyone takes the time to go through it and try to understand this mess, I'd owe you.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct cart {
int id;
char *nume;
} cart;
typedef struct pach {
int id, idCartier, strada, numar, prioritate, codificare;
float greutate;
char* mesaj;
int adresa[18];
} pach;
typedef struct post {
int id, nrPachete;
int vector[50];
} post;
int citeste(int *nrP, cart *cartier, pach *pachet, int *nrC) {
printf("Punctul 1\n");
int i, j;
scanf("%d", nrC);
for (i = 0; i < *nrC; i++) {
cartier[i].id = i;
char aux[500];
scanf("%s", aux);
cartier[i].nume = malloc(strlen(aux) + 1);
cartier[i].nume = aux;
printf("%d %s\n", cartier[i].id, cartier[i].nume);
}
scanf("%d", nrP);
for (i = 0; i < *nrP; i++) {
pachet[i].id = i;
char aux[500];
for (j = 0; j < 18; j++)
scanf("%d", &pachet[i].adresa[j]);
scanf("%d %f", &pachet[i].prioritate, &pachet[i].greutate);
getchar();
fgets(aux, 256, stdin);
pachet[i].mesaj = malloc(strlen(aux) + 1);
pachet[i].mesaj = aux;
printf("%d\n", pachet[i].id);
for (j = 0; j < 18; j++)
printf("%d ", pachet[i].adresa[j]);
printf("\n%d %.6f ", pachet[i].prioritate, pachet[i].greutate);
printf("%s", pachet[i].mesaj);
}
return *nrP;
}
void extrage(int *nrP, pach *pachet) {
printf("\nPunctul 2\n");
int i, j;
for (i = 0; i < *nrP; i++) {
pachet[i].idCartier = 0;
pachet[i].strada = 0;
pachet[i].numar = 0;
for (j = 0; j < 5; j++)
pachet[i].idCartier += pachet[i].adresa[j] * pow(2, (4 - j));
for (j = 5; j < 10; j++)
pachet[i].strada += pachet[i].adresa[j] * pow(2, (9 - j));
for (j = 10; j < 18; j++)
pachet[i].numar += pachet[i].adresa[j] * pow(2, (17 - j));
printf("%d %d ", pachet[i].id, pachet[i].idCartier);
printf("%d %d\n", pachet[i].strada, pachet[i].numar);
}
}
void distribuie(int *nrP, pach *pachet, post *postas, int *nrC, cart *cartier) {
printf("Punctul 3\n");
int i, j;
for (i = 0; i < *nrC; i++) { // FOR-1A
postas[i].nrPachete = 0;
postas[i].id = i;
for (j = 0; j < 50; j++)
postas[i].vector[j] = 0;
}
for (i = 0; i < *nrC; i++) { // FOR-1B
for (j = 0; j < *nrP; j++) {
if (cartier[i].id == pachet[j].idCartier) {
postas[i].vector[postas[i].nrPachete] = pachet[j].id;
postas[i].nrPachete++;
}
}
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
void ordoneaza(pach *pachet, int *nrC, post *postas) {
printf("Punctul 4\n");
pach aux;
int i, j, k = 0, schimbat = 1;
for (i = 0; i < *nrC; i++) {
while (schimbat) {
schimbat = 0;
for (j = 0; j < postas[i].nrPachete - k; j++)
if (pachet[postas[i].vector[j]].prioritate < pachet[postas[i].vector[j+1]].prioritate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[j+1]];
pachet[postas[i].vector[j+1]] = aux;
schimbat = 1;
}
k++;
}
k = 0;
schimbat = 1;
for (j = 0; j < postas[i].nrPachete; j++) {
for (k = j; k < postas[i].nrPachete; k++) {
if (pachet[postas[i].vector[j]].prioritate == pachet[postas[i].vector[k]].prioritate)
if (pachet[postas[i].vector[j]].greutate < pachet[postas[i].vector[k]].greutate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[k]];
pachet[postas[i].vector[k]] = aux;
}
}
}
}
for (i = 0; i < *nrC; i++)
for (j = 0; j < postas[i].nrPachete; j++) {
postas[i].vector[j] = pachet[postas[i].vector[j]].id;
}
for (i = 0; i < *nrC; i++) {
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
int main() {
int nrP, nrC;
pach pachet[1600];
post postas[32];
cart cartier[32];
citeste(&nrP, &cartier[32], &pachet[1600], &nrC);
extrage(&nrP, &pachet[1600]);
distribuie(&nrP, &pachet[1600], &postas[32], &nrC, &cartier[32]);
ordoneaza(&pachet[1600], &nrC, &postas[32]);
return (0);
}
Short info on what the program does:
The citeste function should read the cartier and pachet structures. All of them. And then print those in a bit different format.
The extrage function should take every pachet, and use the adresa (written in BINARY) to convert its 3 parts and obtain the strada, numar and idCartier. Then also print those.
Distribuie checks if the pachet is distributed to a postas (distributed means pachet.idCartier == postas.id), if not it distributes it.
Ordoneaza takes every postas's vector and sorts it after the prioritate (or greutate if the prioritate-s are equal).
But it doesn't work as intended and also gives weird Segmentation Faults.
For example if I comment out the distribuie function, it gives me segfault right after extrage. If I put it back, it gives segfault right after doing it. And if I uncomment everything, it gives segfault at the end again.
If anyone actually read all of this and would be willing to reply, I'd highly appreciate it. Any bit of advice helps!
I did not read your code, but your title said you had trouble passing array of structures. I am attaching a working snippet hope it will help you get around your problem.
#include<stdio.h>
typedef struct employee{
int empId;
char name[10];
}EMP;
void arrayOfStruct(EMP *a, int size)
{
printf("%d\t%d",a[0].empId,a[3].empId);
}
int main()
{
EMP NC[4];
NC[0].empId = 9;
NC[3].empId = 2;
arrayOfStruct(&NC[0],sizeof(NC)/sizeof(NC[0]));
}
with the help of size you can never go beyond the memory allocated for structures.
In case you, want to pass higher dimensional arrays, you have to hard code all the size of arrays except the outer most.
void arrayOfStruct(EMP a[][4], int size)
{
// to do
}
int main()
{
EMP NC[2][4];
...
arrayOfStruct(NC,sizeof(NC)/sizeof(NC[0]));
}
as you see, I did not specify the higher most size of array, which I am passing via other arguement.
Why do I need to specify size of inner dimensions ?
Lets take an example, for suppose you have int[4][4], and you are trying to pass array to a function via int[3][], how does a compiler know how many inner blocks to create, in other case via int[][3], the compiler can easily understand that it has to make inner block of size 3 for each outer array.

Resources