Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,008 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,404 people online right now. Registration is fast and FREE... Join Now!




Array and length help

 
Reply to this topicStart new topic

Array and length help

intaminfan628
27 Nov, 2006 - 09:55 PM
Post #1

New D.I.C Head
*

Joined: 9 Nov, 2006
Posts: 8


My Contributions
CODE

#include <stdio.h>
#include <math.h>

/* function prototypes */
void prob1(void);
void prob2(void);
void prob3(void);

/* add here additional function prototypes as needed */

main()
{
        int menu;
        printf("Enter the function number to execute (1-3):");
        scanf("%d", &menu);
        switch(menu){
         case 1:
            prob1();
            break;
         case 2:
            prob2();
            break;
         case 3:
            prob3();
            break;
         default:
            printf("prob%d() does not exist.\n", menu);
        }
        exit(0);
}
void prob1(void)
{
//printf(" Put your solution for problem 1 here:\n");

        double a_1, b_1, c_1, a_2, b_2, c_2;
        double D, D_1, D_2, x_1, x_2, y, y_1;

        printf("\nEnter a_1, b_1, c_1\n");
        scanf("%lf %lf %lf", &a_1, &b_1, &c_1);

        printf("\n\nEnter a_2, b_2, c_2\n");
        scanf("%lf %lf %lf", &a_2, &b_2, &c_2);
        printf("\n\n");
        D=a_1*b_2-b_1*a_2;
        D_1=c_1*b_2-b_1*c_2;
        D_2=a_1*c_2-c_1*a_2;
        x_1=D_1/D;
        x_2=D_2/D;
        c_1=a_1*x_1+b_1*x_2;
        c_2=a_2*x_1+b_2*x_2;
        y=sqrt(pow(x_1, 2)+pow(x_2, 2));
        y_1=log10(y);
        printf("x_1 = %f,     x_2 = %f", x_1, x_2);
        if(D=0) printf("The equations are linearly dependent\n");
        else printf("\n\nThe value of y is %f\n", y_1);
}

void prob2(void)
{
//printf(" Put your solution for problem 2 here:\n");

        int num, row, num2, *v;
        double  num1, sum;
        char text[80];
        FILE *infile, *outfile;
        infile = fopen("data.dat", "r");
        outfile = fopen("output2.dat", "w");
        if (infile == NULL)
        {
                printf(" Error Opening file!");
                exit(0);
        }


        fscanf(infile, "%d", &num);
//      printf("%d\n", num);
        v = (int *)calloc((size_t)num, sizeof(int *));
        if( v == NULL)
        {
                printf("error");
                exit (0);
        }

        printf("v[] = {");
        for(num2=0; num2<num; num2++)
        {
                fscanf(infile, "%lf", &v[num2]);
                printf("%.2f ", v[num2]);
//              sum += v[num2] * v[num2];
        }
        sum = 0;
        for(num2=0; num2<num; num2++)
        {       sum = sum + pow(v[num2], 2);
        }

        printf("}\n\n");
        printf("\nsum = %f\n", sum);
        sum = sqrt(sum);


        printf("The length of the vector v[] is %f\n", sum);
}

void prob3(void)
{
printf(" Put your solution for problem 3 here:\n");

}


Hi, 2 questions
I am trying to run prob2. However, when i try and calculate the length of the array, it turns out to be a number that is like 15 digits long when the length should relaly be 8.42585. Not sure why it isn't working out.

Also, the second part or prob2 which i have yet to write involves rearranging those 10 numbers in ascending order, how do i write a program that will arrange it for me?

Thank you!
User is offlineProfile CardPM
+Quote Post

horace
RE: Array And Length Help
27 Nov, 2006 - 11:26 PM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
in function prob2() you declare v as a int* (pointer to int) so
CODE

        int num, row, num2, *v;

but when you read the value from data.dat you use the %lf conversion specification which is used to read double value
CODE

   fscanf(infile, "%lf", &v[num2]);
   printf("%.2f ", v[num2]);

change this to
CODE

   fscanf(infile, "%d", &v[num2]);
   printf("%d ", v[num2]);

and it may work (or change v to be a double *)

This post has been edited by horace: 28 Nov, 2006 - 05:04 AM
User is offlineProfile CardPM
+Quote Post

intaminfan628
RE: Array And Length Help
28 Nov, 2006 - 02:21 PM
Post #3

New D.I.C Head
*

Joined: 9 Nov, 2006
Posts: 8


My Contributions
Great, but do you know how to rearrange the vectors in ascending order, right now they are all random. Not sure how to do it, not even sure where to start. But thanks the above solution worked.
User is offlineProfile CardPM
+Quote Post

horace
RE: Array And Length Help
28 Nov, 2006 - 11:19 PM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(intaminfan628 @ 28 Nov, 2006 - 10:21 PM) *

Great, but do you know how to rearrange the vectors in ascending order, right now they are all random. Not sure how to do it, not even sure where to start. But thanks the above solution worked.

you can use the STL sort() to sort a vector, see
http://en.wikipedia.org/wiki/Sort_(C++)

to call sort you pass it iterators to the start and end elements to be sorted
CODE

    sort(v.begin(), v.end());

User is offlineProfile CardPM
+Quote Post

intaminfan628
RE: Array And Length Help
29 Nov, 2006 - 09:41 AM
Post #5

New D.I.C Head
*

Joined: 9 Nov, 2006
Posts: 8


My Contributions
Great, but i don't think my professor doesn't want use to use that way since we don't know that method. The TA did mention something about using swaps not sure how though
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month