I'm new to C and need some help to understand how this piece of code works. I know that it reads the values that the user writes, puts them into an array, and then prints them out.
But I don't understand why I need two "counters" (i and j) to do this. Can someone help me to figure it out?
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
int j=0;
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
while (j < 5)
{
j++;
printf ("\n%d\n", A[j]);
}
}
Technically, what you have aren't two counters, but two loops. If you wanted, to, you could just reuse i for the second loop as well, by doing something like this:
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
i = 0;
while (i < 5)
{
i++;
printf ("\n%d\n", A[i]);
}
As for why you have two loops, the reason is simple. The first loop (using i in your code), reads the 5 integers into the array A. After the first loop concludes, your array A holds the 5 int values, which you could've used however you wanted. In your case, you want to print those values. So what you do is use a loop for looping over the array elements and printing the values to the screen, one by one.
You don't need it, you can simply reset the first and reuse it. However you must increment your index only after having using it otherwise you will overflow the limit of the array :
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
while (i < 5) {
printf("Enter your %d number\n", i);
scanf("%d", &A[i]); // the last must be 4 not 5
i++; //<== increment here
}
i=0;
while (i < 5)
{
printf ("\n%d\n", A[i]); //idem
i++;
}
}
I am doing an exercise that consists of a program that generates 100 random numbers ranging from 1 to 1000 and saves them in an array.
I have to separate this array into two arrays, one for even numbers and one for odd numbers and print the lists.
I have managed to separate into two arrays, but when they are empty, they invent them. How can i avoid it? Any clue to improve it?
Thank you so much.
That's my code:
#include<stdio.h>
#include<conio.h>
int main(){
int array100[100];
int arraypar[100];
int arrayimpar[100];
int i, n=0, m=0;
for(i=0;i<100;i++){
array100[i] = rand() % (1000 + 1);
printf("%i ", array100[i]);
}
printf("\n\n");
for(i=0;i<100;i++){
if(array100[i]%2==0){
m++;
arraypar[m] = array100[i];
}
else{
n++;
arrayimpar[n] = array100[i];
}
}
printf("\n\n Numeros pares: \n");
for(i=1;i<=100;i++){
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i=1;i<=100;i++){
printf("%i ", arrayimpar[i]);
}
getch();
return 0;
}
Un-initialized variables can contain anything, it is better to start of by initializing.
This
int array100[100];
int arraypar[100];
int arrayimpar[100];
Should be:
int array100[100] = {0};
int arraypar[100] {0};
int arrayimpar[100] = {0};
And since C uses 0 base array indexing, you are probably seeing a run time error; Dereference of out-of-bounds pointer (or similar) if the code shown in your post is the actual code you are using.
This:
for(i=1;i<=100;i++){
^ ^^
printf("%i ", arraypar[i]);
}
Should be this:
for(i=0;i<100;i++){
^ ^
printf("%i ", arraypar[i]);
}
Same for the next for loop.
Also, a suggestion to help you troubleshoot. I would start off by initializing array100 with a known set of sequential values to easily test whether the values are being split properly:
int array100[100] = {1,2,6,4,5,6,7,8,9,10,
11,12,16,14,15,16,17,18,19,
20,21,22,26,24,25,26,27,28,29,
60,61,62,66,64,65,66,67,68,69,
40,41,42,46,44,45,46,47,48,49,
50,51,52,55,54,55,56,57,58,59,
60,61,62,66,64,65,66,67,68,69,
70,71,72,77,74,75,77,77,78,79,
80,81,82,88,84,85,88,87,88,89,
90,91,92,99,94,95,99,97,98,99,100};
Now run your program with the rand() section commented out to see what you get. If there are problems, they will be easier to see then looking at a collection of randomly generated numbers.
Index of array starts from 0 not 1, so your code below:
for(i=0;i<100;i++){
if(array100[i]%2==0){
m++;
arraypar[m] = array100[i];
}
else{
n++;
arrayimpar[n] = array100[i];
}
}
should change to:
for(i=0;i<100;i++){
if(array100[i]%2==0){
arraypar[m] = array100[i];
m++;
}
else{
arrayimpar[n] = array100[i];
n++;
}
}
So, when you print, you should print m even numbers and n even numbers instead of print all values in arraypar and arrayimpar.:
printf("\n\n Numeros pares: \n");
for(i=0;i < m;i++){ // if m==0, you print nothing
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i=0;i< n;i++){ // if n == 0, then you print nothing
printf("%i ", arrayimpar[i]);
}
You allocate 100 places for the arrayimpar and arraypar (with indexes 0..99). Usable number of elements (those you want to read) in those arrays are: m(for arraypar) and n (for arrayimpar). Why not use those variables when printing those arrays:
printf("\n\n Numeros pares: \n");
for(i = 1; i <= m; i++){
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i = 1; i <= n; i++){
printf("%i ", arrayimpar[i]);
}
Practically, this should work, but theoretically you could get one of the arrays completely full, and the other empty. When that happens, eventually you will access 101-st element (with index 100) which doesn't exist. Try to fill these arrays from index 0 instead of 1, so you could never access 101-st element.
My sorting program results in a "segmentation fault 11":
#include <stdio.h>
int main()
{
// Asking user for number of inputs in an array
int n;
do {
printf ("enter the number of intigers you want to sort\n");
scanf("%d",&n);
}while (n<=1);
int sort [n];
printf ("please enter %d numbers\n",n);
for (int i=0; i<n; i++) {
scanf("%d",&sort[i]);
}
printf("you entered\n ");
for (int i=0; i<n; i++) {
printf(" %d ",sort[i]);
}
printf("\n");
int k,c,i,x;
for (i=0;i<n;i++) {
if (sort[i]<sort[i-1]){
k=i-2;
while (sort[k]>sort[i]){
k--;
}
k++;
x =sort[i];
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
}
}
printf ("Sorted numbers :-\n");
for (int i=0; i<n; i++) {
printf ("%d ",sort[i]);
}
printf ("\n");
return 0;
}
Now I have looked up the internet and found that it is caused because a variable has value that exceeds the system's memory limit. But cannot understand that concept.
for (i=0;i<n;i++)
{
if (sort[i]<sort[i-1])
You are accessing array out of its bounds.Maybe you want to start your loop from 1.Also
k=i-2;
while (sort[k]>sort[i])
will access index beyond 0 for example if i is 0 or 1 or 2
k = i - 2; looks unstable for low values of i.
sort[i - 1] is undefined when i is zero.
The thing causes the behaviour of sort[k] to be undefined.
Moral of the story: check all the array indexes before attempting to retrieve an array element. Your debugger will help you here.
In addition to all said before:
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
i can be 0 -> c can be 0 -> sort[c-1] would also result in accessing array out of bounds.
There is no need to initialize c=i twice. It is enough to do it in the loop-declaration
Okay so I am trying to make my program ask for user integers (all working fine) and then call the function, which adds 5 to each array element, which i think is fine? then i need to print in the main program: the original integers with 5 added to them. anyone see what is wrong? my program crashes
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(&ARR1[i]);
printf("The new numbers are: %i", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
ARR2[20];
for(i=0;i<20;i++)
ARR2[i]+=5;
}
When you reach this line
FUN(&ARR1[i]);
the value of your i variable is 20 so you are accessing the 21st element in ARR which is probably giving you an access violation. If you replace this line with
FUN(ARR1);
and remove the line
ARR2[20];
from your function FUN you might get the behaviour you are expecting
After your first for loop, i get's the value 20 but ARR1 is indexed 0 to 19. So, you might get a Segmentation Fault when you call
FUN(&ARR1[i]);
You should change that to
FUN(ARR1);
Also, in your function void FUN(int ARR2[]) , you have
ARR2[20];
Which also causes this error. Remove it.
You are also giving output the wrong way. You are doing
printf("The new numbers are: %i", ARR1[i]);
which also tries to access ARR1[20] , again causing problems.
You should change it to
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
You should change your code to
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(ARR1);
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
for(i=0;i<20;i++)
ARR2[i]+=5;
}
What am I doing wrong? the program run but the output isn't in the right order.
#include <stdio.h>
int main () { /*program to ask user to input array values and sort them in ascending order.*/
int n,j,i,temp;
printf ("how many numbers?\n");
scanf ("%d",&n);
int a[n];
for (i=0;i<n;i++){
printf ("enter number");
scanf ("%d",&a[i]);
}
for (i=0;i<n;i++); {
for (j=i+1;j<n;j++){
if (a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp; }
} }
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", a[i]);
}
Change
for (i=0;i<n;i++); {
To
for (i=0;i<n-1;i++) {
Semicolon was executed instead of following błock {}
Element form right/end cant propagate to left, because for example first element of array can be swapped only at first check with second element.