Thread: Help with Arrays!

  1. #1
    Registered User
    Join Date
    Nov 2008

    Help with Arrays!

    I just started programming a few months ago and we have a project for school with the following instructions.
    Read values of carModel, skidlength and roadSlope for a sequence of cases until the value of skidlength = 999 is entered, and for each case, compute the carís speed.
    b) Store the values of CarModel, Skidlength, RoadSlope and Speed as elements of arrays named carModelArray, sLengthArray, rSlopeArray and
    c) After the value Skidlength = 999 has been entered, the algorithm must print the values, from the arrays, in the following layout:
    | Skidlength RoadSlope Speed
    | 20.10 25 26.453
    (the numbers shown are example values)
    You may specify decimal places according to your own wishes
    N.B. The line is a part of the output and the decimal places of Speed,
    Skidlength, and RoadSlope should be 3, 2, 0 respectively.

    I don't know why but I can't seem to get the values to save in the arrays or print from it. Can some please help me? I really need it!

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    int main ()
    double pi=3.1416, brakeFactor=0.7, slopeRad, slopeTangent, speedArray[200]={0}, sum, speed, sLength, sLengthArray[200]={0};
    char carModel[1000], carModelArray[200][1000];
    int rSlope, count=0, rSlopeArray[200]={0},i;
    while (sLength != 999)
    printf ("Please enter the car model.\n");
    scanf ("%s", &carModel);
    printf ("Please enter a skid length.If 999 is entered, no calculations will be done.\n");
    scanf ("%lf", &sLength);
    while (sLength < 0)
    printf ("Please enter a valid skid length.If 999 is entered, no calculations will be done\n");
    scanf ("%lf", &sLength);
    if (sLength == 999)
    printf ("Please enter the road slope.\n");
    scanf ("%i", &rSlope);
    while (rSlope<-30||rSlope>30)
    printf ("Please enter a road slope between -30 and 30.\n");
    scanf ("%i", &rSlope);
    slopeRad = rSlope*(pi/180);
    slopeTangent= sin(slopeRad)/cos(slopeRad);
    speed = sqrt(30*sLength*(slopeTangent + brakeFactor));
    sum = sum + speed;
    count = count + 1;
    for (i=0; i<count; i++)
    sLengthArray[i]= sLength;
    rSlopeArray[i]= rSlope;
    speedArray[i]= speed;
    printf ("|\n");
    printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
    printf ("|\t%.3lf\t%i\t\t%.2lf", sLengthArray, rSlopeArray, speedArray);
    printf ("\n|\n");
    system ("pause");
    return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    1. There is such a function as tan, you know.
    2. Your for loop puts count copies of the same data into every slot of your array. You only want to write one copy of the data, and you want to make sure it's in a different place than all the other data you've written.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    You know it would be that MUCH easier to read if you put an extra CR between blocks of code, and did some indenting:

    for (i stuff) {
           your         // for example                       // look here     
           code       // if i stuff is wrong                // or move on
           here         // this might be irrelevent
    if (condition) {
           even            // supposedly this has less to do   
           more           // with that other stuff than it does with itself
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    London, UK
    You don't initialise SLength and then immediately use it to test a loop condition. If you don't initialise a variable, it could literally be anything, including 999 in this case. ALL your variables should be initialised!

    edit: Oh yeah and your indentation and layout suck!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM