Help with C- simple qns

This is a discussion on Help with C- simple qns within the C Programming forums, part of the General Programming Boards category; Hi all i just wanted to kno how to multiply every second digit by 2 or 3? and then check ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Help with C- simple qns

    Hi all

    i just wanted to kno how to multiply every second digit by 2 or 3?

    and then check if its in increasing order


    so its:
    //first scan the user input

    num[i]= n;

    /* now how to multiply the 2nd by 2 ? */

    checking if its order:

    if(num[i] > num[i+1]){
    etc....

    also is there any website that shows how to read a file and search the file WITHOUT USING THE STRING.H LIBRARY?

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine a for loop (for instance), with one number being incremented by two, every loop:

    number += 2;

    Instead of by just one.

    You'll have to use some logic to decide when the program should multiply by two, and when it should multiply by 3.

    Are you familiar with the C modulus operator (%). It's very handy for logic like this, but I can't tell from your code, just what you want, yet.

    Work on it by hand a bit (paper and pencil), and re-post your newer attempt, when it's ready. The more specific your question, the more helpful we can be.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    hi

    thnks for ur reply

    i understand what u mean...

    and here is my complete code
    Code:
    //reading the integers
    
       printf("Enter 10 integers:\n");
       for (i=0; i < 10; i=i+1) {
          scanf("%d", &n);
          num[i]=n;
     }
    now here i am getting the nos by the user- which works ok, but now i want to multiply every second by 2. How can i do that? and then after multiplying it i want to see if its in order:
    Code:
     int order(int nos[], int lenth) {
         int i, retval=0, temp;
    
         for (i=0; (i < lenth-1) && (retval == 0); i=i+1) {
    //checks the ints and sorts them
            if (nos[i] > nos[i+1]) {
               temp=nos[i];
               nos[i]=nos[i+1];
               nos[i+1]=temp;
    so i dont kno how to x the 2nd digit by 2

    also i dont know how to search for a string/array without using the string.h library?

    thank u very much

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by papermate View Post
    hi

    thnks for ur reply

    i understand what u mean...

    and here is my complete code
    Code:
    //reading the integers
    
       printf("Enter 10 integers:\n");
       for (i=0; i < 10; i=i+1) {
          scanf("%d", &n);
          num[i]=n;
     }
    now here i am getting the nos by the user- which works ok, but now i want to multiply every second by 2. How can i do that? and then after multiplying it i want to see if its in order:
    Code:
     int order(int nos[], int lenth) {
         int i, retval=0, temp;
    
         for (i=0; (i < lenth-1) && (retval == 0); i=i+1) {
    //checks the ints and sorts them
            if (nos[i] > nos[i+1]) {
               temp=nos[i];
               nos[i]=nos[i+1];
               nos[i+1]=temp;
    so i dont kno how to x the 2nd digit by 2

    also i dont know how to search for a string/array without using the string.h library?

    thank u very much
    Doing something with every second number in this case, might mean using the modulus operator: %

    Code:
    if(num % 2 == 0)
      //do something, num is an even number
    num could be an index number for your nos array. I'm unclear what "length" is in your code. For sorting I'd use a simple Selection sort:

    Code:
    for(i = 0; i < NumberOfNumbers -1; i++) {
      for(j = i+1; j < NumberOfNumbers; j++) {
         if(array[i] > array[j]) {
           temp = array[i];
           array[i] = array[j];
           array[j] = temp;
        }
      }
    }
    I'd suggest also putting a getchar() right after your scanf()'s. Here, it stops the very annoying problem of the user puts in a letter, instead of a number, and now the program loops straight up to 10 inputs, all garbage.

    Other times a getchar() after a scanf() will save the day (especially true for char inputs in a program).

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Here is the first part!!!

    Here is the first part!!!



    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int n,m,o,num[20],i=0,j;
    	scanf("%d",&n);
    	m=n;
    	o=n;
    	while(o>0)
    	{
    		o=o/10;
    		num[i++]=o%10;
    		o=o/10;
    	}
    	for(j=0;j<=i-1;j++)
    	printf("%d\t",num[j]*2);
    	getch();
    }

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Probably this is what u want!!!

    Finalized!!!
    Code:
    // 2nddidit.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include<stdio.h>
    #include<conio.h>
    int chkasc(int num[],int n);
    void main()
    {
    	int n,m,o,num[20],i=0,j;
    	scanf("%d",&n);
    	m=n;
    	o=n;
    	while(o>0)
    	{
    		o=o/10;
    		num[i++]=o%10;
    		o=o/10;
    	}
    	for(j=0;j<=i-1;j++)
    	printf("%d\t",num[j]*2);
    	if(chkasc(num,i-2)==0)
    		printf("The values are in ascending order");
    	else
    		printf("The values are not in ascending order");
    	getch();
    }
    int chkasc(int num[],int n)
    {
    	int hold=-999,i;
    	for(i=0;i<=n;i++)
    	{
    		if(num[i]>hold)
    		{
    			hold=num[i];
    			continue;
    		}
    		else
    		{
    			return 1;
    		}
    	}
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Hi

    thanks a lot for ur help!!!!

    Also i am not sure how to search without using the string functions....

    this is what i have so far:
    Code:
    void search(char text[][MAX_LEN+1], char rep[]);
    
          if(argc>2) {
            c= getchar();
    again, thanks for ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21