Reputation: 123
I seem to be having a problem with my code. I have included some details about my assignment below and also my try at the assignment. My professor says I'm very close to the answer but I finally got my code to compile and BAM! I get an infinite loop spouting numbers galore onto my terminal. Help would be very much appreciated! :)
Here are the requirements for my program:
Write a program to find the length of the longest consecutive sub-sequence (ascending order). Output the length, where it's located, and the integers in the sub-sequence. If there is more than one sub-sequence with the same length, then output the first one found.
Input:
The input contains multiple data sets. Each line of the input represents a data set. The first integer in the data set is the number of integers remaining in the data set (on the line). The end of the data sets are marked with a -1.Following is an example of a data file.
16 45 89 41 55 59 64 80 70 12 45 70 90 94 99 23 41 10 1 2 3 4 5 6 7 8 9 10 16 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 -1
Output:
- Data Set 1 ==>
Longest: 6 Positions: 8-13 Sequence: 12 45 70 90 94 99
- Data Set 2 ==>
Longest: 10 Positions: 0-9 Sequence: 1 2 3 4 5 6 7 8 9 10
- Data Set 3 ==>
Longest: 4 Positions: 0-3 Sequence: 1 2 3 4
Your program must contain a main function, at least two more functions, and the usage of an array. Please note, you are not sorting the array, however the Histogram program would be an excellent program to reference.
Let me strongly recommend you create a plan with pencil and paper before you attempt to enter the program. Make sure you mimic my documentation found in the Histogram example.
Here is my code so far:
#include<stdio.h>
#include<stdlib.h>
void load(int a[],int n);
void seq(int a[],int n,int *max, int *loc);
void print(int a[],int n);
int main(void)
{
int a[100];
int n;
int max;
int loc;
scanf("%d",&n);
while(n!=-1){
if(n>100){
fprintf(stderr,"Number entered is larger than 100\n");
exit(1);
}
load(a,n);
seq(a,n,&max,&loc);
print(a,n);
scanf("%d",&n);
}
return 0;
}
void load(int a[],int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void seq(int a[],int n,int *max, int *loc)
{
int i;
int length=1;
*max=-1;
for(i=0;i<n-1;i++){
if(a[i]<a[i+1]){
length++;
if(length>*max){
*max=length;
*loc=i-*max+1;
}
}else{
if(length>*max){
*max=length;
*loc=i-*max+1;
}
length=1;
}
}
}
void print(int a[],int n)
{
int i=0;
while(i<n){
printf("%d ",a[i]);
i++;
}
printf("\n");
}
Upvotes: 0
Views: 239
Reputation: 3742
I'm not going to provide the answer as that probably defeats the point of the assignment (and looks like some effort)... rather let me describe some debugging approaches you should take.
Assuming you're working in a decent development environment (eg. Visual C++ or Visual Studio Express or something), put breakpoints at the start/end of the the while {}
loop and see if the values are what you expect at each stage. You may find the values are not what you expect, which will illuminate the problem.
If you can't debug it in this way, then print debug messages to the console at significant lines of code outputting the states of the variables, and see if they are what you expect. Perhaps force the while {}
loop to break after 10 iterations to avoid the infinite loop and find out what the problem is.
If you aren't equipped with a decent development studio then download Visual Studio 2013 Express for Windows. Trying to debug code without proper tools is pointless and will not teach you any problem solving or programming skills.
Upvotes: 3
Reputation: 123
FINALLY FINISHED!!!!! THANKS FOR YOUR HELP!!! :) :)
/* Name:
* Class: CSC-1710
* Date: 11/11/2013
* File:
*
* This program takes a set of numbers that are no larger than 100
* elements in size and then finds a sequence within the set of numbers
* that are in increasing order and displays the longest sequence,
* the location of the sequence and when it ends.
*/
#include<stdio.h>
#include<stdlib.h>
/* PreCondition:
* Input array will be empty.
* PostCondition:
* Array will be loaded with a maximum of 100 ints.
* The function loads integers from a file.
*/
void load(int a[],int n);
/* PreCondition:
* Array is loaded with n integers.
* PostCondition:
* Array is filtered through sequence function.
* The sequence function takes an array and scans for
* the longest sequence and finds the max and the location.
*/
void seq(int a[],int n,int *max, int *loc);
/* PreCondition:
* Array's max and location of sequence have been found.
* PostCondition:
* Array is printed out with max and location and sequence.
* The function prints the max, location of the beginning of the max,
* and the ending of the max sequence,
* and then uses a for loop to output the sequence that is the longest.
*/
void print(int a[],int n,int max,int loc,int cnt);
int main(void)
{
int a[100];
int n;
int max;
int loc;
int cnt=0;
scanf("%d",&n);
while(n!=-1){
if(n>100){
fprintf(stderr,"Number entered is larger than 100\n");
exit(1);
}
load(a,n);
seq(a,n,&max,&loc);
cnt++;
print(a,n,max,loc,cnt);
scanf("%d",&n);
}
return 0;
}
void load(int a[],int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void seq(int a[],int n,int *max, int *loc)
{
int i;
int length=1;
*max=-1;
for(i=0;i<n-1;i++){
if(a[i]<a[i+1]){
length++;
}else{
if(length>*max){
*max=length;
*loc=i-*max+1;
}
length=1;
}
}
if(length>*max){
*max=length;
*loc=i-*max+1;
}
}
void print(int a[],int n,int max,int loc,int cnt)
{
int b=max+loc;
int i;
printf("Data Set %d ==>",cnt);
printf(" Longest: %d ",max);
printf("Positions: %d-%d ",loc,loc+max-1);
printf("Sequence: ");
for(i=loc;i<b;i++){
printf("%d ",a[i]);
}
printf("\n");
}
Upvotes: 0