Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...

#include <stdio.h>
#include <stdlib.h> // required for atoi
int main(void) {
    int i=0,n,num,filenum[100],pos;
    int c;
   char line[100]; //declaring string for storing data in the line of text
   FILE *fp; // declaring a FILE pointer
   fp=fopen("numbers.txt","r"); // open a text file for reading
   while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file
        filenum[i]=atoi(line); //converting data in the line to integer and storing it into array
       i++;
   }
   n=i;
   fclose(fp); //closing file
   printf("Enter a number: ");
   scanf("%d",&num);
   while(num!=EOF) //looping until user enters end of the file
   {
       pos=0;
       for(i=0;i<n;i++)
       {
           if(num==filenum[i])   //if num is equal to filenum[i]
           {
               pos=i+1;
           }
       }
       if(pos>0)
           printf("%d last appears in the file at position %d\n",num,pos);
       else
           printf("%d does not appear in the file\n",num);
       printf("Enter a number: ");
       scanf("%d",&num);  
              
   }
    return 0;
}

=====================================================

(does anyone can help me to change this program in C++ and add some comments)

numbers.txt:
10
23
43
5
12
23
9
8
10
1
16
9

you must to have all of following output:

Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:

Solutions

Expert Solution

Here is code:

#include <iostream>

#include <fstream>

using namespace std;

int main(void)

{

    int i = 0, n, num, filenum[100], pos;

    int c;

    char line[100]; //declaring string for storing data in the line of text

    ifstream fin("numbers.txt");

    int a;

    while (fin >> a) // read the number untill EOF

    {                   // looping until end of the file

        filenum[i] = a; //converting data in the line to integer and storing it into array

        i++;

    }

    n = i;

    fin.close();

    cout << "Enter a number: ";

    cin >> num;

    while (num != EOF) //looping until user enters end of the file

    {

        pos = 0;

        for (i = 0; i < n; i++)

        {

            if (num == filenum[i]) //if num is equal to filenum[i]

            {

                pos = i + 1;

            }

        }

        if (pos > 0)

            cout << num << " last appears in the file at position " << pos << "\n";

        else

            cout << num << " does not appear in the file\n";

        cout << "Enter a number: ";

        cin >> num;

    }

    return 0;

}

Output:


Related Solutions

example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT