good all
Someone help me with this exercise, in C:
Implement a program that runs as a list number n n places to the left (if n <0) or right (if n> 0).
Input data:
6
JM
OP
TR
HJ
LF
BG
-2
Output:TR HJ LF BG JM OP
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj Kumar ChoudharyPosted Feb 24, 2015, 2:53 PM
#include
#include
#include
int main(void)
{
clrscr();
int n,c,o;
printf("enter your Number \n"); //Here put no of element in your array
scanf("%d",&n);
printf("enter size of each string \n"); //put size of each element
scanf("%d",&c);
char *r=new char[c];
char **p=new char*[n];
for(int i=0;i
p[i]=new char[2];
printf(" Enter Your %d Value \n",i+1);
scanf("%s",p[i]);
}
printf("Enter your ordinate \n");
scanf("%d",&o);
if((o>=0 && o<=n)||(o<=0 && 0>=-6))
{
if(o<0)
{
o=-o;
while(o>0)
{
r=p[0];
for(int i=0;i
p[i] =p[i+1];
}
p[i]=r;
o--;
}
}
else
{
while(o>0)
{
r=p[n-1];
for(int i=n-1;i>0;i--)
{
p[i] =p[i-1];
}
p[i]=r;
o--;
}
}
for(int i=0;i
}
for(int i=0;i
delete *p;
getch();
return 0;
}
in this programe i use the new operator which is part of c++ so for c language you can use malloc
VulpesPosted Feb 24, 2015, 11:16 AM
Jignesh TrivediPosted Feb 24, 2015, 5:03 AM
This is C# code you may convert it into C code by replacing Console.WriteLine to printf() and Console.ReadLine to scanf()
and int.TryParse(Console.ReadLine(), out data); must be replace with scanf("%d", &data);
hope this will help you.
Jignesh TrivediPosted Feb 24, 2015, 4:59 AM
hi,
Try following code
Console.WriteLine("enter the number of contacts you want to enter:");
int data = 0;
int.TryParse(Console.ReadLine(), out data);
string[] allData = new string[data];
for (var i = 1; i <= data; i++)
{
Console.WriteLine(i + " contact:");
allData[i - 1] = Console.ReadLine();
}
Console.WriteLine("ordination:");
int o;
int.TryParse(Console.ReadLine(), out o);
for (var i = 1; i <= data; i++)
{
Console.WriteLine(allData[o]);
o = o + 1;
if(o>=data)
{
o = 0;
}
}
hope this will help you.
Vitor FerreiraPosted Feb 24, 2015, 3:54 AM
enter the number of contacts you want to enter:
6
1 contact: JM
2 contact: OP
3 contact: TR
4 contact: HJ
5 contact: LF
6 contact: BG
ordination: -2
Output: TR HJ LF BG JM OP
The contacts are sorted the TH to OP
Jignesh TrivediPosted Feb 24, 2015, 12:28 AM
can you please provide more information?