LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2010, 07:26 PM   #1
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Rep: Reputation: 1
C: testing multiple consecutive array values concurrently.?


I'm having a bit of trouble figuring out if consecutive values in an array are the same; I don't know how to approach the problem.

Basically, I am provided with a file "temp.dat" with 30 high temperatures (integers) in it. The program is supposed to read them in and compute/print the average. Then it is supposed to print the temperature of each day and, in addition, display a + by each day that is over the average, but only if it is above the average high for three or more consecutive days. This is the part I am stuck on. I'd appreciate any tips that would point me in the right direction

Full disclosure: This is a school project. Code:

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 31

int main() {

  FILE *data;
  int counter, days, i, month_l, sum_t, temperature;
  float average_t = 0;
  int array_t[SIZE];
  char array_c[SIZE];

  i = counter = days = sum_t = 0;

  printHeading();

  printf("Enter the number of days data was collected for:\t");
  scanf("%d",&month_l);

  if ((data = fopen("temp.dat","r")) == 0){
    printf("No such file found. Please place temp.dat in the program's directory.\n");
    exit(1);
  }  

  while(fscanf(data, "%d\n", &temperature) != EOF){
    array_t[i] = temperature;
    sum_t += temperature;
    i++;
  }

  average_t = sum_t / month_l;
  printf("The average temperature is %.2f\n", average_t);

  for(i = 0; i < month_l; i++){
    if(array_t[i] > average_t)
      array_c[i] = '+';
    else
      array_c[i] = ' ';
  }

  for(i = 2; i < month_l; i++){
    if((array_c[i-2] == '+') && (array[i-1] == '+') && (array[i] == '+')){
      //Not really sure what to do here to preserve values for >3 day consecutive above average high temperatures.
    }
      
  }

  for(i = 0; i < month_l; i++){
    printf("%d\t%d  %c\n", ++days, array_t[i], array_c[i]);
  }

}
Here's the data file if you need it:
Code:
85
87
92
95
97
98
90
84
87
93
95
96
96
94
90
91
93
96
94
95
96
94
95
89
89
89
87
97
88
87

Last edited by acvoight; 11-22-2010 at 07:27 PM.
 
Old 11-22-2010, 07:56 PM   #2
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Original Poster
Rep: Reputation: 1
Well I was struck by some inspiration and came up with:

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 31

int main() {

  FILE *data;
  int counter, days, i, month_l, sum_t, temperature;
  float average_t = 0;
  int array_t[SIZE];
  char array_c[SIZE];

  i = counter = days = sum_t = 0;

  printf("Enter the number of days data was collected for:\t");
  scanf("%d",&month_l);

  if ((data = fopen("temp.dat","r")) == 0){
    printf("No such file found. Please place temp.dat in the program's directory.\n");
    exit(1);
  }  

  while(fscanf(data, "%d\n", &temperature) != EOF){
    array_t[i] = temperature;
    sum_t += temperature;
    i++;
  }

  average_t = sum_t / month_l;
  printf("The average temperature is %.2f\n", average_t);

  for(i = 0; i < month_l; i++){
    if(array_t[i] > average_t)
      array_c[i] = '+';
    else
      array_c[i] = ' ';
  }

  for(i = 2; i < month_l; i++){
    if(array_c[i] == '+'){
      if((array_c[i-1] == ' ') && (array_c[i+1] == ' ')){
        array_c[i] = ' ';
      }
      else if((array_c[i-1] == ' ') && (array_c[i+1] == '+')){
        if(array_c[i+2] == '+')
          array_c[i] = '+';
        else
          array_c[i] = ' ';
      }
      else if((array_c[i-1] == '+') && (array_c[i+1] == ' ')){
        if(array_c[i-2] == '+')
          array_c[i] = '+';
        else
          array_c[i] = ' ';
      }
    }
    else
      array_c[i] = ' ';
  }

  for(i = 0; i < month_l; i++){
    printf("%d\t%d  %c\n", ++days, array_t[i], array_c[i]);
  }

}
However, I would still very much appreciate tips on how "you" would solve this as this seems to be the worst way to solve it.

Last edited by acvoight; 11-22-2010 at 08:01 PM.
 
Old 11-22-2010, 09:23 PM   #3
mac.tieu
Member
 
Registered: Jan 2010
Location: Vietnam
Distribution: Arch
Posts: 65

Rep: Reputation: 22
Quote:
Originally Posted by acvoight View Post
However, I would still very much appreciate tips on how "you" would solve this as this seems to be the worst way to solve it.
Code:
  average_t = sum_t / month_l;
  printf("The average temperature is %.2f\n", average_t);

  if (month_l > 0) // lowest item
    array_c[0] = (array_t[0]>average_t)?'+':' ';
  for(i = 1; i < month_l - 1; i++){
    if(array_t[i-1] > average_t) {
      array_c[i-1] = '+';
      if(array_t[i] > average_t && array_t[i+1] > average_t) {
        // Don't need to check these items again
        array_c[i] = '+'; array_c[i+1] = '+'; i+=2;
      }
    } else {
      array_c[i-1] = ' ';
      if (array_t[i+1] < average_t) {
        array_c[i] = ' '; i++;
      }
    }
  }
  // remain items
  if (month_l > 1)
    array_c[month_l-1] = (array_t[month_l-1]>average_t)?'+':' ';
  if (month_l > 2)
    array_c[month_l-2] = (array_t[month_l-2]>average_t)?'+':' ';

  for(i = 0; i < month_l; i++){
    printf("%d\t%d  %c\n", ++days, array_t[i], array_c[i]);
  }
Cheers,
MT.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Preventing multiple instances of a shell script from running concurrently Dave Lerner Programming 17 04-21-2020 12:53 PM
[SOLVED] Shell script to convert values on successive lines into consecutive ranges? kmkocot Programming 5 07-09-2010 10:59 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
Accessing a java array concurrently enemorales Programming 9 11-30-2007 11:24 AM
Testing values in C Machiaveli Programming 5 09-06-2006 01:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration