Question

In: Computer Science

LANGUAGE C Code function: I wanted to create an array, i.e. index[100], and store 3 elements...

LANGUAGE C

Code function:

I wanted to create an array, i.e. index[100], and store 3 elements into it.

The elements that i wanted to store is from the same variable, however its value will always change in the while loop

e.g. index[0]=1, index[1]=3, index[2]=5

However it seems that my code has a problem that my compiler warns me that the variable index is set but not used. And also, is it not possible to store the elements into the array using the while loop in my code? As i am not familiar with c language. Can sir/madam fix the codes below and teaches the proper way?

NOTE: i use gcc compiler in unix environment. Any assistance is very much appreciated in advance

#include <stdio.h>

int main(){
   int c=0;
   int j=2;
   int i=0;
   int index[100];

   int a=1;
  
   while(c<=j){
       index[c]=a;
       a+=2;
       c+=1;

   }

   for(i=0;i++;i<=j){
       printf("%d",index[i]);
   }
}

Solutions

Expert Solution

Well you have done everything correct and while loop is working fine to store elements, only fault was in the for loop which you used for printing.

Updated Code:

#include <stdio.h>

int main(){
   int c=0;
   int j=2;
   int i=0;
   int index[100];

   int a=1;
  
   while(c<=j){
       index[c]=a;
       a+=2;
       c+=1;
   }

   for(i=0;i<=j;i++){ // I changed here
       printf("%d",index[i]);
   }
}

Output:

135

Explanation:

The fault was inside for loop your loop statement was  for(i=0;i++;i<=j) and based on the syntax of for loop it has to be for(i=0;i<=j;i++)

In simple terms to remember the syntax of for loop you consider it to be as 3 part where

1st part in initialization of loop (i=0)

2nd part is like the condition which will terminate the code or stop the for loop when you need it to be stoped (i<=j)

3rd part is like an increment statement for variable of for loop (i++)

If you ever feel like code is not storing value try printing the value at that index for example you know index[0]=1 so you can write printf("%d",index[0]) just to verify if there is a fault in code which is used for printing or in code for storing data in array .


Related Solutions

Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
In java language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
Explain this code: The structure used is max heap using array. C++ (i is the index...
Explain this code: The structure used is max heap using array. C++ (i is the index of the element to be deleted) void del(int i) {    int left,right,temp;    arr[i]=arr[n-1];    n=n-1;    left=2*i+1; /*left child of i*/    right=2*i+2; /* right child of i*/    while(right < n)    {        if( arr[i]>=arr[left] && arr[i]>=arr[right] )            return;        if( arr[right]<=arr[left] )        {            temp=arr[i];            arr[i]=arr[left];   ...
Use C language. I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the...
Use C language. I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
How can I create a hexadecimal number in c code from a specific array. For example...
How can I create a hexadecimal number in c code from a specific array. For example I have a[4]={2,5,7,4}; and I want to create this number : 0x004725 by adding 1 number at a time using << operation. Thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT