Jump to content

Beginner C++ problem

Go to solution Solved by maliki1908,

I'm assuming your goal was to produce the sum of absolute values and the sqrt of the sum of squares respectively. Your program is semantically correct however you have made two mistakes in reading your input. The first is that your fscanf call reads integers (you should have %f not %d). The second is that you are passing a dereferrenced float value as the last param of fscanf, you should be passing in the address. Thus the line should read either

 

fscanf(fin, "%f\n", x+q) 

 

or

 

fscanf(fin, "%f\n", &x[q]) 

 

The first one uses pointer arithmetic to establish the memory location by determining the qth offset of a float stored in x and the second uses standard array access notation to resolve the *value* at the qth location and then derefrence it. Regardless of whether or not you choose to use C or C++ you should have a firm grasp of pointers. I highly recommend you read up on them.

 

P.S

 

You should consider asking this sort of question on a dedicated forum like stack overflow.

 

This is an assignment question, and is most likely a problem with pointers. imput file is :

1
 2 
-1 

-2


#include<stdio.h>
#include<math.h>
#define N 100

void distance(float* x, int q, float* ra, float* rb);

int  main(void){
    
    FILE* fout = fopen("lengthout.txt" , "w");
    FILE* fin = fopen("lengthin.txt", "r");
    
    float x[N], ra, rb;
    int q=0;
    
    while(fscanf(fin, "%d\n", x[q]) !=EOF){
        q++;
    }
    
    distance(x, q, &ra, &rb);
    
    fprintf(fout, "The array has %d entries. The norms are: \n" , q);
    fprintf(fout, "1-norm = %f \n", ra);
    fprintf(fout, "2-norm = %f \n", rb);
    
    fclose(fout);
    fclose(fin);
    
return 0;    
}

void distance(float* x, int q, float* ra, float* rb){

    float pra=0, prb=0;
    int t;
    
    for(t=0;t<q;t++){
        pra = pra + fabs(x[t]);
    }
    *ra = pra;
    
    for(t=0;t<q;t++){
        prb = prb + pow(x[t], 2);
    }
    *rb = sqrt(prb);
}

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/
Share on other sites

Link to post
Share on other sites

First off use the code box which is the "<>" in the editor.

 

Second, what problem are you having exactly? 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856439
Share on other sites

Link to post
Share on other sites

3 minutes ago, PandaCruncher said:

the program stops working as soon as you click run

Well use the code tags and add some comments so we know what's going on. 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856472
Share on other sites

Link to post
Share on other sites

First and foremost, welcome to the programming forum! I know you specified C++ in your OP, but your code can be compiled as plain C. Is this part of the assignment? When writing C++, it's reccommended to use C++ features. Regardless, I'll answer your question directly. First and foremost, the program expects there to a file in the directory that you're executing from named "lengthin.txt". If you're running it from an IDE (you said click run, so I'm assuming you are), then the working directory from your program will probably not be where the source code (.cpp file) is. What IDE are you using? CodeBlocks? Visual Studio? Also, what platform are you using (Windows, Mac, Linux?). These will help.

 

But anyway, it's good practice to check to make sure the file is open before using it. In your program, when the file does not exist, fopen() is going to return a nullptr. Dereferencing a nullptr will cause a "segmentation fault", which means your program tried to access memory that it didn't own. So, always check your pointers, then cancel the program if file opening fails. So your program woudl look this this:


#include<stdio.h>
#include<math.h>
#define N 100

void distance(float* x, int q, float* ra, float* rb);

int  main(void){
    
    FILE* fout = fopen("lengthout.txt" , "w");
    FILE* fin = fopen("lengthin.txt", "r");

    if (!fin) {
        printf("Problem opening file! Program must exit\n");
        return 1;
    }
    
    float x[N], ra, rb;
    int q=0;
    
    while(fscanf(fin, "%d\n", x[q]) != EOF){
        q++;
    }
    
    distance(x, q, &ra, &rb);
    
    fprintf(fout, "The array has %d entries. The norms are: \n" , q);
    fprintf(fout, "1-norm = %f \n", ra);
    fprintf(fout, "2-norm = %f \n", rb);
    
    fclose(fout);
    fclose(fin);
    
return 0;    
}

void distance(float* x, int q, float* ra, float* rb){

    float pra=0, prb=0;
    int t;
    
    for(t=0;t<q;t++){
        pra = pra + fabs(x[t]);
    }
    *ra = pra;
    
    for(t=0;t<q;t++){
        prb = prb + pow(x[t], 2);
    }
    *rb = sqrt(prb);
} 


Unfortunately, after that, I'm not sure exactly what your program is trying to do. Can you explain what you want to do with the info in the lengthin.txt file?

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856509
Share on other sites

Link to post
Share on other sites

I'm assuming your goal was to produce the sum of absolute values and the sqrt of the sum of squares respectively. Your program is semantically correct however you have made two mistakes in reading your input. The first is that your fscanf call reads integers (you should have %f not %d). The second is that you are passing a dereferrenced float value as the last param of fscanf, you should be passing in the address. Thus the line should read either

 

fscanf(fin, "%f\n", x+q) 

 

or

 

fscanf(fin, "%f\n", &x[q]) 

 

The first one uses pointer arithmetic to establish the memory location by determining the qth offset of a float stored in x and the second uses standard array access notation to resolve the *value* at the qth location and then derefrence it. Regardless of whether or not you choose to use C or C++ you should have a firm grasp of pointers. I highly recommend you read up on them.

 

P.S

 

You should consider asking this sort of question on a dedicated forum like stack overflow.

 

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856580
Share on other sites

Link to post
Share on other sites

3 minutes ago, maliki1908 said:

I'm assuming your goal was to produce the sum of absolute values and the sqrt of the sum of squares respectively. Your program is semantically correct however you have made two mistakes in reading your input. The first is that your fscanf call reads integers (you should have %f not %d). The second is that you are passing a dereferrenced float value as the last param of fscanf, you should be passing in the address. Thus the line should read either

 

fscanf(fin, "%f\n", x+q) 

 

or

 

fscanf(fin, "%f\n", &x[q]) 

 

The first one uses pointer arithmetic to establish the memory location by determining the qth offset of a float stored in x and the second uses standard array access notation to resolve the *value* at the qth location and then derefrence it. Regardless of whether or not you choose to use C or C++ you should have a firm grasp of pointers. I highly recommend you read up on them.

 

P.S

 

You should consider asking this sort of question on a dedicated forum like stack overflow.

 

yup that will do it thanks man!

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856608
Share on other sites

Link to post
Share on other sites

Pinguinsan advice is also useful. You should always check the return value of system calls like fopen before proceeding to manipulate them or you will get segfaults if the file being read doesn't exist or some other error is encountered. Out of curiousity what sort of class is this for?

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856636
Share on other sites

Link to post
Share on other sites

Just now, maliki1908 said:

Pinguinsan advice is also useful. You should always check the return value of system calls like fopen before proceeding to manipulate them or you will get segfaults if the file being read doesn't exist or some other error is encountered. Out of curiousity what sort of class is this for?

its a first year university programming coarse

Link to comment
https://linustechtips.com/topic/690399-beginner-c-problem/#findComment-8856650
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×