Question

Hey! i hit a roadblock in my C# program. I am just wondering how i would...

Hey! i hit a roadblock in my C# program. I am just wondering how i would search for a set of 9 numbers within an array and have a message box say if it was there or not. I have it where the contents of the txt file is loaded into an array right when the program starts. Any and all help is greatly appreciated :)

each number in the txt file is like this 123445567 385910475 938405719 503818405

the array i put it into is this

const int SIZE = 18;
int[] numbers = new int[SIZE];

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


/*C# program that read an input file ,data.txt that contains the 18 numbers. into an array of size,18. Then prompt the user to enter the key value to search in the array. If the value is found in the array, then print the location  of value found in the array otherwise print the value is not found .*/

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadTextFile
{
class Program
{
static void Main(string[] args)
{
const int SIZE = 18;
int index = 0;
int[] numbers = new int[SIZE];
//Set file name data.txt
String fileName = "data.txt";

Console.WriteLine("Reading from file ,data.txt values :");
/**Create an instance of StreamReader to data from read fileName */
StreamReader fileReader = new StreamReader(fileName);
string line;
/*Read till end of the file */
while ((line = fileReader.ReadLine()) != null)
{
//read data values from the file into line and parse the value to integer value
int.TryParse(line, out numbers[index]);
index = index + 1;
}

Console.WriteLine("data.txt file data values :");
for (index = 0; index < SIZE; index++)
Console.WriteLine("{0}", numbers[index]);

Console.WriteLine("Enter value to search in the data.txt file:");
//read element to search
int key = int.Parse(Console.ReadLine());

//Set location to -1
int location = -1;
//Set found to false
bool found = false;

/*Checking for the value in the array ,numbers */
for (index = 0; index < SIZE && !found; index++)
{
if (key == numbers[index])
{
//update the found and location values
found = true;
location = index;
}
}

/*Check if boolean found is true*/
if (found)
/*Print the value and location */
Console.WriteLine("Value {0} is found at index locaiton {1} ", key, location + 1);
else
Console.WriteLine("Value {0} is not found." ,key);
Console.ReadKey();
}
}
}

------------------------------------------------------------------------------------------------

Input file : data.txt

18344
38591
93840
50345
12234
33491
93848
50341
12347
38591
93325
50351
12237
23759
93840
50231
12233
38556

Note: data.txt text file can be changed to any values you like upto 18 values.

Please make sure that the data.txt file is in the "bin" folder of the c# project folder.

------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Hey! i hit a roadblock in my C# program. I am just wondering how i would...
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
  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Hey, i was just wondering how i would calculate over time pay in c++ visual studio...

    Hey, i was just wondering how i would calculate over time pay in c++ visual studio 2017? what i have right now is either returning 0 for some reason or being skipped over? im also wondering about the federal tax as well because that also doesn't seem to work, any help is greatly appreciated! these are my variables char chChoice = ' '; int intempID = 0; int intHours = 0; int intOThours = 0; float flOTrate = 0; float...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • I am failing one of the tests cases for the trimArray function. The test that my...

    I am failing one of the tests cases for the trimArray function. The test that my function is failing is testing that when you get a toTrim array of  " ", it should return "" (an array of no characters). How do I incorporate that functionality into the function and where will it go in the C++ code? Here's my function: // The function trimArray() is given a pointer to a character array that // is NULL terminated called toTrim. This...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • I am working in c++. My program requires me to use a header file that is...

    I am working in c++. My program requires me to use a header file that is provided. I must use this header file and create 5 methods: 2 constructors, 2 accessors and this operation* on a 4x4 matrix. Matrix has 16 floating point values that must be entered by user. The Two operator functions: i*t; and t+i, multiply identity (i) matrix by user input value matrix and second function add identity (i) plus user input matrix transform. These two operations...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

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