have been stuck on this for the past few days
my array contains
int a={1,2,3,4,5,6,7,8,9,10,11,12};
I have to split them up into 2 different arrays such that
int b={1,2,3,7,8,9};
int c={4,5,6,10,11,12};
is there any function for this? I have tried using the for loop but I am unable to print c without printing the first few values.
any help would be much appreciated
#include <stdio.h>
int main(){
int a[12];
int b[6];
int c[6];
for(int i=0;i<12;i++){
b[i]=a[i];
for(int b=3;b<12;b++){
c[i]=a[i]
}
}
}
Related
I want to create a program that will print the value of the three parameter by using define in c
#include <stdio.h>
#define Print(num) printf("%d",n##num)
int main()
{
int i;
int n1=1, n2=2, n3=3;
for(i=1;i<=3;i++)Print(i);
}
the problem than n##num equal to ni and to n1,n2,n3.
is there a way to get the values of i to set in num instead of "i"?
The preprocessor runs before the code is even compiled. You can't do loops in the preprocessor like that.
What you really want is an array:
#include <stdio.h>
int main()
{
int i;
int n[] = { 1, 2, 3 };
for(i=0;i<3;i++) printf("%d\n", n[i]);
}
why my code is not printing all values of the array?
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[5]={1,2,3,4,5};
int *a;
a=&arr[0];
int i=0;
for(i=arr[i];i<5;i++){
printf("%d",*a);
a=a+1;
}
}
I guess this is homework, correct? Check the index of your loop. What you want to do is that your for loops five times, regardless of the content of your array.
You are taking the first index of the array as i instead you should take i as 0 so that it will iterate from 0 to 4 taking all index. Instead you are starting from 1 to 4. Here is the code if needed:-
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[5]={1,2,3,4,5};
int *a;
a=&arr[0];
int i;
for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){//You can replace "sizeof(arr)/sizeof(arr[0])" with 5
printf("%d",*a);
a=a+1;
}
}
#include <stdio.h>
#include <stdlib.h>
int smallest(int [],int);
int select_sort(int[],int);
int smallest(int arr[],int len){
int small_index=0;
int small=arr[0];
for(int i=0;i<len;i++){
if(arr[i]<small){small=arr[i];
small_index=i;
}
}
return small_index;
}
int select_sort(int arra[],int len){
int new_arra[100];
for(int i=0;i<len;i++){
int small=smallest(arra,len);
new_arra[i]=arra[small];
printf("%d",new_arra[i]);
}
return new_arra;
}
int main()
{
int arr[100]={6,1,0,-2,18};
select_sort(arr,5);
return 0;
}
I wrote this code for the selection sorting program and i know ideally i should be using the dynamic allocation for arrays in the select_sort function, but i was attempting it without it. It is supposed to print the array in an ascending order and I think I am messing up variable assignment somewhere, because when i run the program it only prints the smallest integer of the input array len number of times and not the rest of them.
If you don't mind messing up with your initial array, you can do:
int select_sort(int arra[],int len)
{
int maxValue = Integer.Max_Value;
int new_arra[100];
for(int i=0;i<len;i++){
int small=smallest(arra,len);
new_arra[i]=arra[small];
arra[small]= maxValue;
printf("%d",new_arra[i]);
}
return new_arra;
bear in mind that this is highly unefficient
#include <stdio.h>
int main() {
char usernames[5][10] = {"Lebron","Davis","Schroder","KCP","Gasol"};
int i = 0;
for(i=0; i < 20; i++) {
printf("%s\n",usernames[i][20]);
}}
Hello guys, I'm new at programming and I study for use for loop with char arrays. You can see my code in top. The output I want to print is;
Lebron
Davis
Schroder
KCP
Gasol
I put my i into char's second dimension, is it not true? Basically, I want to print all the elements in my array, how can I do it? Thanks in advance.
Here you are storing data into 2d array. So, each row will store a sequence of char or a string . like usernames[0] = "Lebron" and so on. So, array length will be 5 . But you are trying to print 20 elements. that will give you runtime error.
here is the solution that will work for you.
#include <stdio.h>
int main() {
char usernames[5][10] = {"Lebron","Davis","Schroder","KCP","Gasol"};
int i = 0;
for(i=0; i < 5; i++) {
printf("%s\n",usernames[i]);
}
return 0;
}
/this code is for rotating the array with n no of entries k no of times
and outputting the array elements at indexz q no of times/ my problem here is that it is showing runtime error why is it happening like that.this question is actually from hacker rank and it is by the name of circular array rotation in implimentation section in algorithms.is there anything wrong in this code.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,k,q;
int a[n];
scanf("%d%d%d",&n,&k,&q);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int j=0;j<k;j++)/*this is for rotating the array*/
{
int y=a[n-1];
for(int x=n-2;x>=0;x--)
a[x+1]=a[x];
a[0]=y;
}
for(int b=0;b<q;b++)
{
int z;
scanf("%d",&z);
printf("%d\n",a[z]);
}
return 0;
}
Problem:
int n,k,q;
int a[n];
You are creating an array of size n before setting the value of n.
Use:
int n,k,q;
// Read a value into n first
if ( scanf("%d%d%d",&n,&k,&q) != 3 )
{
// Deal with error
return 1;
}
// Then define the array.
int a[n];