-1

if array has size 5 and we have put only 2 or 3 integer values then how we could run a loop till the values we entered in C?Actually i am making program in C where i need to run a loop till the number of elements we have in array?

 void main()
 { 
    char a[]="this is a man"; 
    int b[5],j=0,q; 
    int p= sizeof(a)/sizeof(a[0]); 
    for(int i=0;i<=p;i++)
    { 
        if(a[i]=='i')
        { 
            b[j]=i; j++;
        }  
    } 

    for(int k=0;k<=5;k++)
    { 
        printf("value %d\n",b[k]); 
    } 
 } 
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    See this: http://stackoverflow.com/questions/1597830/iterate-through-a-c-array – hunterc Dec 04 '13 at 16:42
  • 2
    ***Show Sample Code***, then we can improve on it, instead of just talking about hypotheticals. – abelenky Dec 04 '13 at 16:46
  • @user2980181 In your loop you use `j` to count, right? Just use it when printing the array `b`. You don't need any magics. – P.P Dec 04 '13 at 17:01
  • thanks haccks..above is a code now tell me what do use in place of "k<=5". –  Dec 04 '13 at 17:03
  • I edited my answer. You can do this without any sentinel value. Read the answer. – haccks Dec 04 '13 at 17:06
  • 1
    `for(int i=0;i < p;i++)` for a start. and `int main(void)` of course – wildplasser Dec 04 '13 at 17:07
  • See this: http://stackoverflow.com/questions/20029783/get-the-number-of-elements-that-it-has-a-value-in-array for my Ten Different Ways (in truth only 9, I'm still thinking of a tenth way). – Jongware Dec 04 '13 at 20:19

2 Answers2

2

Use a sentinel value such as -1 or 0 at the end. Check for that sentinel on each iteration to come out from the loop.

int a[5];  

while(a[i] != -1)  
{
     ...
}   

EDIT: Try this

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char a[]="this is a man";
    int b[5] = {0};    //I initialized all elements of b to 0.
    int j=0;
    int p= sizeof(a)/sizeof(a[0]);
    for(int i=0;i<p;i++)  // I used i < p instead of yours i <= p.
    {
        if(a[i]=='i')
        {
            b[j]=i; j++;
        }
    }

    for(int k=0;k<5;k++)  // k < 5 instead of k <= 5.
    {
        if(b[k])            //Check for the non zero value of b, if yes print that value.
            printf("value %d\n",b[k]);
    }
    return 0;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • its a good suggestion..but problem is that other values are also non zero like "8653" 9103".. –  Dec 04 '13 at 17:15
  • thanks buddy...please let me know how u found that?/ –  Dec 04 '13 at 17:21
  • Care fully read this answer. In this answer I changed many things but most important for your program is I remove `=` from both of your loop which is causing your programs behavior undefined and printing `8653`, `9103` as garbage values. – haccks Dec 04 '13 at 17:21
  • thanks a lot..next time i'll more careful about all these things..thanks. –  Dec 04 '13 at 17:25
  • Your welcome. I glad that my answer helped you :) – haccks Dec 04 '13 at 17:26
  • haccks if u r still there..i want to ask something from u..please reply.. –  Dec 04 '13 at 17:56
  • @user2980181; *thanks buddy...please let me know how u found that?*: By answering couples of question on SO almost everyday :) – haccks Dec 04 '13 at 17:56
  • Yes. I am here. Go ahead. – haccks Dec 04 '13 at 17:57
  • if i delete the all the elements of which i got the position..there is a problem.when we delete one element and move very next element to its place.the next position we got delete the element previous to the position we want.. –  Dec 04 '13 at 18:05
  • Sorry, I do not understand. Please rephrase your words or give some example what you want to do.. – haccks Dec 04 '13 at 18:07
  • i don't have that much repo..to continue comment so long and i can't chat as my repo is low.do u have any other option..well i'll place a code in comment.i don't know how to place it properly here.but i'll try –  Dec 04 '13 at 18:10
  • for(int k=0;k<5;k++) { if(b[k]) { printf("value %d\n",b[k]); for(int c=b[k];c

    –  Dec 04 '13 at 18:11
  • the problem is here that first time it deletes correct word but after that it deletes character before the position we mentioned and so on... –  Dec 04 '13 at 18:15
  • OK. I understand. You want the output `ths s a man`, right? Let me check. – haccks Dec 04 '13 at 18:16
  • Define `int count = 0`. Then do this `for(int k=0;k<5;k++) { if(b[k]) { printf("value %d\n",b[k]); for(int c = b[k] - count; c < p - 1; c++) { a[c]=a[c + 1]; } count++; } }` – haccks Dec 04 '13 at 18:44
  • well tell me way to increase my repo..so i could chat with people like u?? –  Dec 04 '13 at 18:52
  • Yes you can. Ask some good questions but before asking search SO whether it is asked already on SO. Also you can answer some questions if you know the answer. The easiest way to get reputation is edit the questions and answers such as, spelling mistake, grammatical mistakes or code indentation etc. You will get `2` reputation for each edit. :). Read the [about page](http://stackoverflow.com/about) for detailed information. – haccks Dec 04 '13 at 18:56
  • thanks..i'll try.well your language of interest is?? –  Dec 04 '13 at 18:58
  • 1
    NO..i am asking about programming language..well for me its English and Hindi.. –  Dec 04 '13 at 19:01
  • i do want to learn web technologies..i hope u'll help me in it as well..well i am basically involved in android.but nowdays working on C,C++ to understand pointers well.. –  Dec 04 '13 at 19:13
  • Best of luck. Keep in touch with Stack Overflow. – haccks Dec 04 '13 at 19:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42568/discussion-between-user2980181-and-haccks) –  Dec 05 '13 at 09:57
0

You initialize the array with some known placeholder value and check for that placeholder value while iterating each element.

Alok Save
  • 202,538
  • 53
  • 430
  • 533