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, i was wondering how i get data from a .txt file and put it into...

    Hey, i was wondering how i get data from a .txt file and put it into an array? We have to create a structure to hold the information(firstname, lastname, and age) on some characters. That character info is stored on a .txt file. Any and all help is greatly appreciated. (We are using Visual studio 2017 if that makes a difference) txt file Bugs Bunny 1940 Elmer Fudd 1933 Porky Pig 1935 Daffy Duck 1937 Tweety Bird 1942 Taz Devil...

  • 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...

  • 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...

  • 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...

  • 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...

  • Question 8) Suppose a program is supposed to merge two files in the following manner: write...

    Question 8) Suppose a program is supposed to merge two files in the following manner: write two lines from the first file, write two lines from the second file, and repeat the process until all of the lines have been written to the new merged file. For example suppose we have the following two input files: File1.txt File2.txt AAA BBB CCC DDD 000 111 222 333 Then the merged output file would be: File3.txt AAA BBB 000 111 CCC DDD...

  • Hey guys! Huge part of my grade !! My code works already but I need it...

    Hey guys! Huge part of my grade !! My code works already but I need it so it references to my node list class instead of my array, what would I need to change? My assignment  was to create a list from an assignment when we used an array, but I cant make it to work without the array, help! The professor said it was okay to either keep the array or to delete it altogether. package zipcode; import java.io.File; import...

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