Question

C Programming, getting data from a file and computing them into 5 parallel arrays so that they can be used for later calculation.

Also please avoid using structures at all costs for this code as they have not yet been covered.

# Melbourne Central Daily Pedestrian Counts day 2 2009 2009 2009 06 06 06 01 02 03 daycount 22663 22960 23618 4 2018 2018 02 02 27 28 33722 33164 4 There will always be two heading lines in all input files, and then rows of five values separated by tab characters (\t in C). Once the first two lines have been bypassed, each data line can be read using scanf(%d%d%d%d%d , . . . ). The number in the fifth column is the pedestrian count for the day indicated by the dd/mm/yyyy date, with the fourth field, day, indicating the day of the week, numbered from Sunday to Saturday as integers 1 to 7 respectively. You can open these data files using jEdit if you wish to view them, and they can also be manipulated using Excel. Stage 1 - Control of Reading and Printing (marks up to 4/10) Your first program should read the entire input dataset into a collection of five parallel arrays (or, if you are adventurous, an array of struct), counting the data rows as they are read. The two heading lines should be discarded and not processed in any way.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

c code:

#include <stdio.h>
#include <stdlib.h>

//structure pedestrian
struct Pedestrian
{
//variables
int yyyy;
int mm;
int dd;
int day;
int dayCount;
};
int main()
{
struct Pedestrian p[4000]; //array of struct objects
int n=0;
FILE *fptr;
char line[256];

fptr=fopen("pedestrian.tsv","r");//file

if(!fptr)
{
printf("File not found\n");
return 1;
}
//ignore first two lines
fgets(line, sizeof(line), fptr);
fgets(line, sizeof(line), fptr);

//read and store in structure
while(!feof(fptr))
{
fscanf(fptr,"%d%d%d%d%d",&p[n].yyyy, &p[n].mm,&p[n].dd, &p[n].day, &p[n].dayCount);
n++; //line count
}

//print the results
printf("\nS1: Total data lines = %d", n);
printf("\nS1: First data line = %d/%d/%d, %d",p[0].dd,p[0].mm,p[0].yyyy,p[0].dayCount);
printf("\nS1: last data line = %d/%d/%d, %d",p[n-1].dd,p[n-1].mm,p[n-1].yyyy,p[n-1].dayCount);


return 0;
}

output:

İ DcppPedestrainFilebinDebug\PedestrainFile.exe S1: Total data lines-3 1: First data line1/6/2009. 22663 S1 last data line28/

//i have given the output for file with only 3 lines.. you can give n numbers in your file.. it works fine..

for any clarification, do comments, please give me thumbs up..

Add a comment
Know the answer?
Add Answer to:
C Programming, getting data from a file and computing them into 5 parallel arrays so that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that will input data from a Comma-separated values (.csv) file and output som...

    Write a C++ program that will input data from a Comma-separated values (.csv) file and output some information about it. This program uses a csv file from Yahoo Finance (.csv) filename : SBUX.csv 1. Output the name of the ticker to the console screen (without the “.csv”) 2. Output the start date and end date that was found in the file 3. Output how many trading day(s) existed in the file 4. Prompt the use to input a number of...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • Code with Java using arrays and Scanner only ( input should end with 0 to terminate...

    Code with Java using arrays and Scanner only ( input should end with 0 to terminate the program) Everyone loves to be celebrated on their birthdays. Birthday celebration can encourage positive social interaction among co-workers, foster friendship among classmates or even strengthen bond between families. Birthday graph can be display in many forms. It can a creative drawing consists of cupcakes, balloons, candles with names, or it can be in the form of simple bar chart to indicate the birthday...

  • 1. Write a C++ program that reads daily weather observation data from a file and writes...

    1. Write a C++ program that reads daily weather observation data from a file and writes monthly and annual summaries of the daily data to a file. a. The daily weather data will be contained in a file named wx_data.txt, with each line of the file representing the weather data for a single day. b. For each day, the date, precipitation amount, maximum temperature, and minimum temperature will be provided as tab-separated data with the following format: 20180101 0.02 37...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • Need help Purpose Calculate mileage reimbursements using arrays and methods. The Mathematical Association of America hosts...

    Need help Purpose Calculate mileage reimbursements using arrays and methods. The Mathematical Association of America hosts an annual summer meeting. Each state sends one official delegate to the section officers’ meeting at this summer session. The national organization reimburses the official state delegates according to the scale below. Write a Java program to calculate the reimbursement values, satisfying the specifications below. Details on array and method usage follow these specs. 1. The main method should declare all the variables at...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • Introduction to C Programming – COP 3223 1. To learn how to use arrays to store...

    Introduction to C Programming – COP 3223 1. To learn how to use arrays to store and retrieve data to help solving problems. 2. Reinforce use of input files. Introduction: Ninja Academy Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they would like to become a ninja. To amuse your friend, you have decided to create a series of programs about ninjas. Problem: Mentorship (ninjamentors.c) It is time for your friend to select...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT