modular and flexible programming - c++

I am trying to learn how to make a modular program. So what I want to do is read an array of integers.
Main:
#include <stdio.h>
#include <stdlib.h>
#define NMAX 10
void read (int *n, int a[NMAX]);
int main()
{
int n, a[NMAX];
read(&n,a);
return 0;
}
Then I saved this file 'read.cpp':
#include <stdio.h>
#include <stdlib.h>
#define NMAX 10
void read (int *n, int a[NMAX])
{
int i;
printf("dati n:\n");
scanf("%d",n);
for (i=1;i<=*n;i++)
{
printf("a[%d]= ",i);
scanf("%d\n",&a[i]);
}
}
read.cpp compiles succesfully, but when I compile the main function I get the error "no reference to read".

Include read.cpp when compiling.
g++ -o out main.cpp read.cpp
or
add #include "read.cpp" in main program

Related

Is it possible to use C++'s sort in C?

I am writing C code but have to do a lot of calls to qsort which is taking most of the time. I notice that C++'s sort is faster than qsort. Is it possible for me to use it somehow? Here is a MWE in C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main(void) {
int sz;
srand(time(NULL));
printf("Enter the size of array::");
scanf("%d",&sz);
uint32_t *arr = malloc(sz * sizeof *arr);
int i;
for(i=0;i<sz;i++)
arr[i]=rand();
clock_t begin = clock();
qsort(arr, sz, sizeof *arr, cmpfunc);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%f seconds\n", time_spent);
printf("%d\n", arr[10]);
return 0;
}
Yes, it is possible. C++ is designed to handle such things without much hassle. You need to compile the C++ function separately and then link it to your project:
The header should be valid C and C++. You need to use extern "C" by checking if it's C++:
// i32sort.h
#pragma once
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C"
#endif // __cplusplus
void i32sort(int32_t*, size_t);
The source file is straight C++:
// i32sort.cpp
#include "i32sort.h"
#include <algorithm>
extern "C" void i32sort(int32_t* const data, size_t const size)
{
std::sort(data, data + size);
}
Then compile it without linking:
$ clang++ -c i32sort.cpp -O3 -Wall -Wextra
Then include the i32sort.h header in your source file as usual:
// main.c
#include <stdio.h>
#include "i32sort.h"
int main(void)
{
int32_t arr[] = { 5, 4, 3, 2, 1 };
size_t const sz = sizeof (arr) / sizeof (* arr);
i32sort(arr, sz);
for (size_t i = 0; i < sz; ++i)
printf("%d ", (int) arr[i]);
putchar('\n');
}
While compiling your program, link it with the object file you generated previously:
$ clang main.c i32sort.o -O3 -Wall -Wextra
$ ./a
1 2 3 4 5

command spawnl() not executed

I have a code that is executable without error messages but it seems like it denies to run the code
I call by the "spawnl" command. This is my code and I receive "error=-1". I tried may different ways to solve the situation but I always receive the "-1" for an answer. I use Dev C++ compiler with 32bit release. My problem is to call the other program, sending the name of the file.
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <process.h>
#include <ctype.h>
#include <dir.h>
#include <windows.h>
#include <dos.h>
#include <iostream>
#include <stddef.h>
void translate_to_ascii_files(),
save_mesh_data(),
make_no(),
give_names();
int load_patches(char *);
FILE *memco ;
int outnod[400] ;
float r_vector[21][21][3],cx[500],cy[500],cz[500] ;
int NOP1,NOP2; /* Number Of Points */
int patches;
char nams[26][26];
int num_of_files;
int kk,nv,nh,exnod,totnod ;
int no[4][400];
char filename[26];
void *buf;
COORD coord= {0,0};
HANDLE hConsole;
using namespace std; // std::cout, std::cin
void gotoxy(int x,int y) {
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
int main(int argc,char *argv[]) {
char com_nam[26];
int counter1,counter2,i;
int error;
system("cls");
give_names();
printf("give the final filename:");scanf("%s",filename);
for(patches=1;patches<=num_of_files;patches++) {
strcpy(com_nam,nams[patches]);
load_patches(com_nam); /* load patches for meshing*/
i=1;
for(counter1=0;counter1<=NOP1;counter1++) {
for(counter2=0;counter2<=NOP2;counter2++) {
cx[i]=r_vector[counter1][counter2][0];
cy[i]=r_vector[counter1][counter2][1];
cz[i]=r_vector[counter1][counter2][2];
i=i+1;
}
}
nh=NOP1+1;nv=NOP2+1;
make_no();
save_mesh_data();
if(argc ==1){
error=spawnl(P_WAIT,"c:\\cpprog\\unitsrf.exe","",filename,NULL);
if (error ==0) {
else {printf ("error=%d\n",error); system("PAUSE");
} }
else{gotoxy(2,11);printf("error=%s\n\n",argv[1]);
system("PAUSE");
}
} translate_to_ascii_files();
}

Why I get "mpir_ui" was not declared in this scope?

after installing mpir-3.0 on fedora 31. Now I try to build project:
#include <stdio.h>
#include <gmp.h>
#include <mpir.h>
#include <mpfr.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
mpf_t a; //mpir float variable
mpf_init(a); //initialise a
mpir_ui two = 2; //mpir unsigned integer variable
FILE* stream; //file type pointer to output on standard output (console)
mpf_init_set_ui (a, 2); //set value of a to 2
mpf_out_str (stream, 10, 2, a); //output value of a
cout << "\nMPIR working" << "\n" ;
}
But when I compile it I get this error:
‘mpir_ui’ was not declared in this scope; did you mean ‘mpfr_ai’?|
I've used the flags:
-lmpir -lmpfr -lgmp

Huge ammount of errors during compilation of program with Magick++

During the compilation of a program I was making, that uses the Magick++ library, there appeared an incredibily huge ammount of errors.
This is the command line I used for compile it (g++'s version is 4.9.2, running on Ubuntu 12.04):
g++ -std=c++14 -g -fopenmp `Magick++-config --cppflags --cxxflags` main.cpp -o glitch_img2 `Magick++-config --ldflags --libs`
Here is the program's code:
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <string>
#ifdef __OPENMP
#include <omp.h>
#endif
#include <Magick++.h>
double d_max(double a,double b)
{
return (a>b) ? a : b;
}
int ui_clip(int x,int a,int b)
{
return (x<b) ? b : ((x>a) ? a : x);
}
int transform(int x)
{
return ((x*32+1654)%21+x*11+642)%164%33%16;
}
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
InitializeMagick(*argv);
Image img1;
img1.read(argv[1]);
int size_x = img1.columns();
int size_y = img1.rows();
int np = size_x*size_y;
Image out;
out.size(Geometry(size_x,size_y));
double *buff_img1 = new double[3*size_x*size_y];
double *buff_out = new double[3*size_x*size_y];
img1.write(0,0,size_x,size_y,"RGB",DoublePixel,buff_img1);
int ix, iy;
#ifdef __OPENMP
#pragma omp for private(ix,iy) schedule(static)
#endif
for (iy=0;iy<size_y;iy++)
{
for (ix=0;ix<size_x;ix++)
{
int loc = (size_x*iy+ix)*3;
double pimg1[3] = {
buff_img1[3*loc ],
buff_img1[3*loc+1],
buff_img1[3*loc+2]
};
int x1 = (ix*(1+transform(ix)))%size_x;
int y1 = (iy*(1+transform(iy)))%size_y;
int loc1 = ui_clip((size_x*y1+x1)%np,0,np-1)*3;
double out_pix[3] = {
buff_img1[3*loc1 ],
buff_img1[3*loc1+1],
buff_img1[3*loc1+2]
};
buff_out[3*loc ] = abs(out_pix[0]-pimg1[0]);
buff_out[3*loc+1] = abs(out_pix[1]-pimg1[1]);
buff_out[3*loc+2] = abs(out_pix[2]-pimg1[2]);
}
}
out.read(size_x,size_y,"RGB",DoublePixel,buff_out);
out.write("Output.png");
}
And those errors are posted here.
What causes such errors and how can they be fixed?
EDIT: The mentioned behaviour dissapears when I remove the Magick++.h header.

GMP Library - File I/O

Okay. So I'm using GMP library to calculate big numbers. I've got code like this:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <gmp.h>
using std::cout;
using std::endl;
int main(int argc, char** argv)
{
FILE *file;
file = fopen("data.txt", "wt");
int number=atoi(argv[1]), i=1;
mpz_t a; mpz_init(a);
mpz_t b; mpz_init(b);
mpz_set_ui(b, 1);
cout<<a<<endl;
for (; number>0; number--, i++)
{
cout<<i<<". "<<b<<endl;
mpz_add(b,b,a);
mpz_sub(a,b,a);
}
mpz_clear(a);
mpz_clear(b);
fclose(file);
}
And I wanted to print numbers (a,b) to a .txt file. How can I do it? Tried fprintf(), but it doesn't seem to work
You should use gmp_fprintf().
The format specifier will be %Zd for mpz_t, so the code will be like
gmp_fprintf(file, "%Zd\n%Zd\n", a, b);
Other format specifiers and samples are in GNU MP 6.1.0: Formatted Output Strings