Thread: Terminator in integer Array.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    8

    Question Terminator in integer Array.

    Hello

    I have to create program in which user has to choose how many numbers he would like to enter for example 3. then program ask about these numbers.(Array needs to have space=100) After that program has to add the terminator with value (-1) on the next free space in the array. (that is my main problem).

    For example, if the user enters that they have 5 values, the system will then ask them5 times to enter a number to be placed in the arrayOnce the user has finished entering their values, the system should add a -1 in to thenext free element of the array. This will act as a 'terminator'.

    in the same program system should ask the user what element of the arraythey want to replace. It should then ask what number they want to put in theirchosen array element.The program should then replace the users chosen array element with the value theyentered.

    Main problem which i have i do not know how to use terminator, when i cant declare variables like int /code a[5]={1,2,3,4\-1};, like a[100]; and user has to enter the values. /code

    Code:
    #include <stdio.h>
    #include <ctype.h>
    void As5GradeB()
    {
    
      int a[100];
      char opt;
      int i;
      float mean;
      int sum = 0;
      int num;
    
      opt = tolower(opt);
      printf("Enter number of elements \n");
      scanf("%d", &num);
      for (i = 0; i < num; i = i + 1) {
        printf("Enter value of the elements%d\n", i + 1);
        scanf("%d", &a);
      }
    
    
      while ((opt = getchar()) != '\n' && opt != EOF);
    
      while (opt != 'x') {
    
    
        printf("\n Please enter your choice from the following menu:\n");
        printf("A – Repopulate array \n");
        printf("B - Display all values \n");
        printf("C – Replace one number \n");
        printf("D – Calculate the mean \n");
        printf("E - Find largest number\n");
        printf("X - Exit the program \n");
        scanf("%c", &opt);
    
        opt = tolower(opt);
        if (opt == 'a') {
          for (i = 0; i < num; i = i + 1) { // reading 5 values from user and storing it in array
            printf(" Enter the value number %d\n\n", i + 1);
            scanf("%d", &a);
          }
          printf("The new array is : \n");
          for (i = 0; i < num; i++) {
            printf("%d\n", a);
          }
        } else if (opt == 'b') {
          printf("You choose to display all values \n\n");
          for (i = 0; i < num; i++) {
            printf("%d\n", a);
          }
        } else if (opt == 'c') {
          int ele;
          int val;
          printf("You choose to replace one number\n");
          printf("Which element of the array You want to replace ? \n");
          scanf("%d", &ele);
          printf("What is the replaced value ?");
          scanf("\n%d", &val);
          for (i = 0; i < 100; i++) {
            if (a == ele) {
              a = val;
            }
          }
          printf("%d", a);
        } else if (opt == 'd') {
          sum = 0;
          for (i = 0; i < num; ++i) {
            sum += a;
          }
          mean = sum / num;
          printf
              ("Mean = sum of  the elements devided on the number of elements\n = %.2f\n",
               mean);
        }
    
        else if (opt == 'x') {
          printf("Thank you for using the program");
          break;
        } else if (opt == 'e') {
          printf("You choosed to find the largest element  \n\n");
          // storing the largest number to arr[0]
          for (i = 0; i < num; ++i) {
            if (a[0] < a)
              a[0] = a;
          }
          printf("Largest element = %d\n", a[0]);
        } else {
          printf("Wrong menu option- try again\n");
        }
        while ((opt = getchar()) != '\n' && opt != EOF);
    
    
      }
    }
    Last edited by Salem; 05-24-2020 at 07:14 AM. Reason: Fixed crayola formatting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. integer null terminator
    By vead in forum C Programming
    Replies: 6
    Last Post: 02-11-2018, 08:42 PM
  2. function terminator
    By programmerc in forum C Programming
    Replies: 3
    Last Post: 02-17-2013, 08:08 AM
  3. array and string terminator
    By bobknows in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2011, 07:45 PM
  4. Terminator 4
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-23-2009, 11:15 PM
  5. Terminator 3..nope
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 07-18-2003, 07:42 AM

Tags for this Thread