Need help with arrays and linear searches

I have an assignment due shortly and need some help.

Here is the assignment information :

For this assignment, you’ll need to:

1. Develop a short C++ program.
2. Compile it.
3. Run it & debug if necessary.
4. Submit the working C++ source code file in D2L as Programming Assignment 1.

The “Writing Programs” document under Content in D2L has information about writing your programs for this course. If you want to work on your own computer, you need to install an application, like CodeBlocks to write your programs with. There are tutorials about installing CodeBlocks and using it to complete this assignment in D2L under Content/Tutorials.

Here’s your assignment:


1. Read from 0 to 150 integers from a file named “Temps.txt” and place the contents into an array of integers.
2. Your program must now display the numbers 5 per line on the monitor.
3. Prompt the user for a temperature they would like to search for.
4. Use linear search to find the temperature in the array.
5. Display a message stating at which index the value was found or that it was not found.
6. Sort the contents of the array in ascending order.
7. Display the sorted array with 5 numbers per line.
8. Compute the average temperature and display the results on the monitory.
9. Submit your working C++ file as Programming Assignment 1 in D2L. Refer to the Student D2L 101 course or Student Guides on the Course Home page in D2L for more information on how to do this.

Additional Requirements:
• A named constant MAXSIZE must be used in the program
• Make sure and check for and handle an empty data file
• The file may have more than 150 values, so you must check for this condition
• Include all needed error checking
• Use functions appropriately
• Include required comments/documentation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// preprocessor directives
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>

// user-defined constants

// user-define string data type - room for NULL character
int main()
{
    const int MAXSIZE = 150;
    int numbers[MAXSIZE];
    int count = 0;
    ifstream inputFile;

    inputFile.open("Temps.txt");
	
    while (count < MAXSIZE && inputFile >> numbers[count]) 
	
	{
        count++;
	}
    
	inputFile.close();

	
    for (int index = 0; index < count; index++)
	{
        cout << numbers(index) << " ";
	}
	 
    return 0;
}
1
2
3
4
5
    while (count < MAXSIZE && inputFile >> numbers[count]) 
	
	{
        count++;
	}


• Make sure and check for and handle an empty data file
• The file may have more than 150 values, so you must check for this condition

A simple way to check the above conditions would be to check the value of count.

1
2
3
4
    for (int index = 0; index < count; index++)
	{
        cout << numbers(index) << " ";
	}


2. Your program must now display the numbers 5 per line on the monitor.

Use the modulus operator(%) to print 5 numbers every line.

1
2
3
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 

You don't seem to be using any functions from these headers, so you should remove them.
1
2
3
4
    for (int index = 0; index < count; index++)
	{
        cout << numbers(index) << " ";
	}

Don't do that. This code attempts to read exactly count numbers. The original code is fine, just be sure to check whether you read zero numbers after closing the input file.

2. Your program must now display the numbers 5 per line on the monitor.
7. Display the sorted array with 5 numbers per line.

Since you have to print the array twice, put that code in a function.

Here is a skeleton of code that you must right, complete with functions. Search for "todo" and fill in the code in those areas.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// preprocessor directives
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>

using std::cout;
using std::ifstream;

// user-defined constants

// user-define string data type - room for NULL character

void printArray(int numbers[], int count)
{
    // TODO: print 5 per line.
   for (int index = 0; index < count; index++) {
	cout << numbers[index] << " ";
   }
   cout << '\n';
}

// Given an array "numbers" of "count" integers, find "key".
// Return the index of "key", or -1 if not found.
int findValue(int numbers[], int count, int key)
{
    // 4. Use linear search to find the temperature in the array.
    // TODO
}

// Given an array "numbers" of "count" integers, compute and return
// the average
double computeAverage(int numbers[], int count)
{
    // TODO
}


int
main()
{
    const int MAXSIZE = 150;
    int numbers[MAXSIZE];
    int count = 0;
    ifstream inputFile;

    inputFile.open("Temps.txt");

    while (count < MAXSIZE && inputFile >> numbers[count])
    {
	count++;
    }

    inputFile.close();

    // TODO: check for zero numbers entered.

    printArray(numbers, count);

    int temperature;
    // TODO: Prompt the user for a temperature they would like to search for.
    
    int idx = findValue(numbers, count, temperature);
    
    // 	TODO Display a message stating at which index the value was
    // 	found or that it was not found.

    // TODO: Sort the contents of the array in ascending order.

    // 7. Display the sorted array with 5 numbers per line.
    printArray(numbers, count);

    // 8. Compute the average temperature and display the results on
    // the monitory.
    double average = computeAverage(numbers, count);
    // TODO: display the aveage

    return 0;
}

> cout << numbers(index) << " ";
> This code attempts to read exactly count numbers.
that code doesn't read.

by the way, they should be brackets [], no parenthesis ().
Topic archived. No new replies allowed.